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