微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何以编程方式安装下载的 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 举报,一经查实,本站将立刻删除。