关于android:Android调用系统安装apk的注意事项

6次阅读

共计 1533 个字符,预计需要花费 4 分钟才能阅读完成。

对于 7.0 及其以上的设施咱们须要做如下操作:
1. 在 manifest 中注册 FileProvider

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

2、指定可用的文件门路
在我的项目的 res 目录下,创立 xml 文件夹,并新建一个 file_paths.xml 文件。通过这个文件来指定文件门路:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Context.getFilesDir() 位于 /data/data/ 装置目录 -->
    <files-path name="internalPath" path="file" />
    <!--Context.getCacheDir()-->
    <cache-path name="cachePath" path="file" />
    <!--Environment.getExternalStorageDirectory()-->
    <external-path name="externalPath" path="file" />
    <!--Context.getExternalFilesDir(null)-->
    <external-files-path name="externalFPath" path="file" />
</paths>

3、调用代码:

File apkFile = new File(apkPath);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    Uri contentUri = FileProvider.getUriForFile(this, BuildConfig.FileProvider,apkFile);
    intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
}
startActivity(intent);

个别网上的教程就到此为止,然而当初局部手机出于平安思考,禁止了 APK 自在调用安装程序,因而,还须要在 AndroidManifest 中增加一条权限:

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

原文链接:https://blog.csdn.net/w815878…

正文完
 0