7.0自动更新的问题(完整方案)

问题所在

事情是这样的,前几天,发现有几个手机升级应用失败,查了下,发现都是7.0的机器,于是查了下google的7.0API描述,发现了这个东西


google_doc

ps:google的文档中文化真的很高,现在。

完整解决方案

以前我们是怎么处理下载问题的呢?

下载

private static void downloadNewVersionApp(Context context, String url) {
  DownloadManager downloadManager =
      (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
  Uri uri = Uri.parse(url);
  DownloadManager.Request request = new DownloadManager.Request(uri);
  request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, getUpdateFile());
  request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
      | DownloadManager.Request.NETWORK_WIFI);
  request.setVisibleInDownloadsUi(false);
  long id = downloadManager.enqueue(request);
  AppCache.appDownloadId = id;
}

下载完成回调

private DownloadManager downloadManager;

@Override
public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
    long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
    if (id != AppCache.appDownloadId) {
      return;
    }
    IntentUtils.installApk(context);
  }
}

安装

public static void installApk(Context context) {
  File file= new File(
      Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
      , FishUtils.getUpdateFile());
  if (file == null) return;
  if (!file.exists()) return;
  Intent intent = new Intent();
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  intent.setAction(Intent.ACTION_VIEW);
  intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
  context.startActivity(intent);
}

可以看到以前我们确实是用URI传递地址了,但是现在不行了。所以,要判断版本

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M){
      installAPKGreaterThanM(context,file);
      return;
    }

7.0 后更新

因为googleAPI的限制,这里我们需要使用FileProvider

<provider
      android:name="android.support.v4.content.FileProvider"
      android:authorities="com.XXXX.fileprovider"
      android:grantUriPermissions="true"
      android:exported="false">
      <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_paths" />
  </provider>

exported:要求必须为false,为true则会报安全异常。
grantUriPermissions:true,表示授予 URI 临时访问权限。
authorities 组件标识,按照江湖规矩,都以包名开头,避免和其它应用发生冲突。

看下我们指定的@xml/file_paths

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
  <external-path path="" name="download"/>
</paths>
</resources>

这里的path=""指的是根目录,即可以其他应用共享根目录和子目录下的所有资源

恩,然后看下怎么使用

private static void installAPKGreaterThanM(Context context,File file) {
  Uri apkUri =
      FileProvider.getUriForFile(context, "com.XXXX.fileprovider", file);
  Intent intent = new Intent(Intent.ACTION_VIEW);
  // 由于没有在Activity环境下启动Activity,设置下面的标签
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  //添加这一句表示对目标应用临时授权该Uri所代表的文件
  intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
  context.startActivity(intent);
}

大功告成!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容