zoukankan      html  css  js  c++  java
  • 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         }

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

  • 相关阅读:
    禁止在工作流设计器启动持续活动的重新编译
    设计流程 工作流
    workflow 工作流
    访问调度控制 时间控件
    如何:实现一个视图项目
    Python多线程之threading.Thread实现
    gcc 编译流程分析
    如何编写Makefile?
    linux 文件夹的颜色代表什么意思
    STL容器的迭代器失效的原因
  • 原文地址:https://www.cnblogs.com/liyiran/p/6289450.html
Copyright © 2011-2022 走看看