解决Android更新安装包时不能自动安装的问题
  ttOzQgS7km1w 2023年12月12日 36 0


 

一,安装代码

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

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

<?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>

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月12日 0

暂无评论

推荐阅读
  anLrwkgbyYZS   2023年12月30日   34   0   0 ideciciMaxideMax
ttOzQgS7km1w