解决Android更新安装包时不能自动安装的问题

版权声明:转载请@我原创地址 https://blog.csdn.net/weixin_39706415/article/details/85158404

一,安装代码

   private void installUseAS(String filePath) {
        File file = new File(filePath);
        Uri uri = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileprovider", file);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        mContext.startActivity(intent);
    }

二,适配7.0代码FileProvider

1,Minifest中配置

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

2,新建file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="my_images"
        path="images/" />
    <files-path
        name="name"
        path="path" />
    //相当 Context.getFilesDir() + path, name是分享url的一部分

    <cache-path
        name="name"
        path="path" />
    //getCacheDir()

    <external-path
        name="name"
        path="path" />
    //Environment.getExternalStorageDirectory()
    <external-path
        name="download"
        path="Download" />
    <external-files-path
        name="name"
        path="path" />//getExternalFilesDir(String) Context.getExternalFilesDir(null)

    <external-cache-path
        name="name"
        path="path" />
    //Context.getExternalCacheDir()
</paths>

猜你喜欢

转载自blog.csdn.net/weixin_39706415/article/details/85158404