0

Following is the code to download and install apk file when download is completed . but i cant open the apk file after download.

private fun downloadAPk(apkUrl: String) {
        val request = DownloadManager.Request(Uri.parse(apkUrl))
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
        request.setTitle("Customer Information")
        request.setDescription("Downloading...")
  
        request.setDestinationInExternalFilesDir(this,"CI","CustomerInformation.apk")
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        val manager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        isDownloaded = manager.enqueue(request)
        val broadcast = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1)
                if (id == isDownloaded) {
                    val install = Intent(Intent.ACTION_VIEW);
                    install.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP;
                    val uri  = ? // dont know how to get path of where file is downloaded
                    install.setDataAndType(Uri.parse(uri), manager.getMimeTypeForDownloadedFile(isDownloaded));
                    startActivity(install);
                    unregisterReceiver(this);
                    finish()
                }
            }
        }
        registerReceiver(broadcast, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
    }

i dont know if the code i used in "override onReceive()" will open & install the apk file... any suggessions how to open that downloaded APK file to install it? plz

Asfir Ali
  • 1
  • 1
  • 1
    Have you tried this? https://stackoverflow.com/questions/4967669/android-install-apk-programmatically – Syed Danish Ali Feb 13 '21 at 12:18
  • yes i have .. Environment.getExternalStorageDirectory() used in that code is deprecated. i cant specify the exact path where downloaded file is stored . – Asfir Ali Feb 15 '21 at 04:08
  • So what's the issue here? Are you not able to install the apk or unable to get the downloads directory? For the latter one, look here. https://stackoverflow.com/questions/7908193/how-to-access-downloads-folder-in-android – Syed Danish Ali Feb 15 '21 at 07:35
  • i fixed that issue , i was unable to specify the directory ... – Asfir Ali Feb 16 '21 at 05:50

0 Answers0