如何以编程方式安装下载的 APK 文件?

问题描述

以下是下载完成后下载安装apk文件代码。但我下载后无法打开apk文件

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))
    }

我不知道我在“覆盖 onReceive()”中使用的代码是否会打开并安装 apk 文件...任何建议如何打开下载的 APK 文件进行安装?请你

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)