美文网首页
Android 7.0+ 弹出安装应用

Android 7.0+ 弹出安装应用

作者: kedwan | 来源:发表于2018-09-01 15:11 被阅读14次

1、在 AndroidManifest.xml 中添加权限 android.permission.REQUEST_INSTALL_PACKAGES
2、在 AndroidManifest.xml 的application中添加下面代码:

<application>
    ......
    <provider
            android:exported="false"
            android:grantUriPermissions="true"
            android:authorities="[你的包名].provider"
            android:name="android.support.v4.content.FileProvider">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_file" />
        </provider>
</application>

3、在res的xml中新建provider_file.xml资源文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <root-path name="root" path="" />
    <files-path name="files_path" path="." />
    <cache-path name="cache_path" path="." />
    <external-path name="external_path" path="." />
    <external-files-path name="external_files_path" path="." />
    <external-cache-path name="external_cache_path" path="." />
</paths>

4、在Application的onCreate添加如下代码:

public MyApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                StrictMode.setVmPolicy(builder.build());
        }
    }
}

5、弹出安装App:

public void toInstallApp(Context context,File file){
       ......
       Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data;
        String type = "application/vnd.android.package-archive";
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            data = Uri.fromFile(file);
        } else {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String authority = Utils.getApp().getPackageName() + ".provider";
            data = FileProvider.getUriForFile(Utils.getApp(), authority, file);
        }
        intent.setDataAndType(data, type);
        intent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
}

相关文章

网友评论

      本文标题:Android 7.0+ 弹出安装应用

      本文链接:https://www.haomeiwen.com/subject/ojhuwftx.html