安卓7.0手机上遇到的app升级问题终极解决方案

来源:互联网 发布:淘宝内裤店铺起名 编辑:程序博客网 时间:2024/05/18 03:45
 兼容Android 7.0 App升级

一、在AndroidManifest.xml清单文件中注册Provider

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

authorities: 常用命名为包名+fileprovider
exported要求必须为false,为true会报安全异常
grantUriPermissions表示URI临时访问权限,true为需要临时访问权限

resource指当前组件引用res文件夹中qk_file_paths文件,这个文件名可以随便写,只要在res文件夹中有这个文件即可。

在res文件夹中新建xml文件夹,再新建cui_file_paths.xml文件,内容如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
        <external-path
            name="files_root"
            path="Android/data/com.cuileikun.androidbase/" />
        <external-path
            name="external_storage_root"
            path="." />
        <!-- yan  notice:  do not  edit     -->
        <root-path path="" name="camera_photos" />
    </paths>
</resources>

path填上Android/data/包名

二、安装Apk时注意兼容高低版本

Intent intent = new Intent(Intent.ACTION_VIEW);
//判断是否是AndroidN以及更高的版本
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
         intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
         Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", apkFile);
         intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
      } else {
         intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      }
      context.startActivity(intent);

主要是注意getUriForFile方法中的参数,第二个参数为BuildConfig.APPLICATION_ID + “.fileprovider”

即包名+.fileprovider,这个和AndroidManifest中的Provider中的属性authorities一致


参考链接:

http://www.2cto.com/kf/201704/622561.html

阅读全文
1 0