APK 自动安装

2019-02-19  本文已影响0人  NullPoint3Exce

1.Android 7.0之前

Intent intent = new Intent(Intent.ACTION_VIEW);
// 由于没有在Activity环境下启动Activity,设置下面的标签
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(new File(apkPath)),
                    "application/vnd.android.package-archive");context.startActivity(intent);
 

2.Android 7.0之后
在Android7.0之后的版本运行上述代码会出现 android.os.FileUriExposedException
“私有目录被限制访问”是指在Android7.0中为了提高私有文件的安全性,面向 Android N 或更高版本的应用私有目录将被限制访问。
FileProvider

<provider
   android:name="android.support.v4.content.FileProvider"
   android:authorities="com.example.chenfengyao.installapkdemo"
   android:grantUriPermissions="true"
   android:exported="false">
   <!--元数据-->
   <meta-data
       android:name="android.support.FILE_PROVIDER_PATHS"
       android:resource="@xml/file_path" />
</provider>

需要注意一下几点:

  1. exported:必须为false
  2. grantUriPermissions:true,表示授予 URI 临时访问权限。
  3. authorities 组件标识,都以包名开头,避免和其它应用发生冲突。
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <paths>
        <external-path path="" name="download"/>
    </paths>
</resources>

path=”“,是有特殊意义的,它代表根目录,也就是说你可以向其它的应用共享根目录及其子目录下任何一个文件了.

Intent intent = new Intent(Intent.ACTION_VIEW);
File file = (new File(apkPath));
// 由于没有在Activity环境下启动Activity,设置下面的标签
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致   参数3  共享的文件
Uri apkUri = FileProvider.getUriForFile(context, "com.example.chenfengyao.installapkdemo", file);
//添加这一句表示对目标应用临时授权该Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
context.startActivity(intent);
 

最后,兼容使用

//普通安装
private static void installNormal(Context context,String apkPath) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    //版本在7.0以上是不能直接通过uri访问的
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        File file = (new File(apkPath));
        // 由于没有在Activity环境下启动Activity,设置下面的标签
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致   参数3  共享的文件
        Uri apkUri = FileProvider.getUriForFile(context, "com.example.chenfengyao.installapkdemo", file);
        //添加这一句表示对目标应用临时授权该Uri所代表的文件
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
    } else {
        intent.setDataAndType(Uri.fromFile(new File(apkPath)),
                "application/vnd.android.package-archive");
    }
    context.startActivity(intent);
}
 

上一篇下一篇

猜你喜欢

热点阅读