Android:使用 DownloadManager 进行版本更新,出现 No Activity found to handle Intent 及解决办法...

项目中,进行版本更新的时候,用的是自己写的下载方案,最近看到了使用系统服务 DownloadManager 进行版本更新,自己也试试。

在下载完成以后,安装更新的时候,出现了一个 crash,抓取的 log :

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW type=application/vnd.android.package-archive flg=0x10000000 }

代码:

1             Intent install = new Intent(Intent.ACTION_VIEW);
2             DownloadManager mManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
3             Uri downloadFileUri = mManager.getUriForDownloadedFile(downloadApkId);
4             install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
5             install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
6             context.startActivity(install);

 

通过搜索发现应该是传入的 Uri 有问题,安装 apk 的 Uri 应该是 file:// 开头的,但是代码中获取的 Uri 是 content://

修改后的代码:

 1         Intent install = new Intent(Intent.ACTION_VIEW);
 2         Uri downloadFileUri;
 3         File file = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/update.apk");
 4         if (file != null) {
 5             String path = file.getAbsolutePath();
 6             downloadFileUri = Uri.parse("file://" + path);
 7             install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
 8             install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 9             context.startActivity(install);
10         }

修改后的代码可以正常进行版本更新了。

 

转载于:https://www.cnblogs.com/liyiran/p/6289450.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值