相关配置
AndroidManifest.xml
<!--相关权限-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application android:networkSecurityConfig="@xml/network_security_config">
<provider
android:name="androidx.core.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>
</application>
创建network_security_config.xml,解决Android9.0明文请求
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
创建file_paths.xml,解决Android7.0 uri共享文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--
代表 context.getFileDir()
文件保存路径为:/data/应用包名/files/
Uri:content://${applicationId}.provider/fileDir/
-->
<files-path
name="fileDir"
path="." />
<!--
代表 context.getExternalFilesDir()
文件保存路径为:/storage/emulated/0/Android/data/应用包名/files/
Uri:content://${applicationId}.provider/filePath/
-->
<external-files-path
name="filePath"
path="." />
<!--
代表 context.getCacheDir()
文件保存路径为:/data/应用包名/cache/
Uri:content://${applicationId}.provider/cache-path/
-->
<!--name自定义 .为根路径-->
<cache-path
name="cache-path"
path="." />
<!--
代表 getExternalCacheDir()
文件保存路径为:/storage/emulated/0/Android/data/应用包名/cache/
Uri:content://${applicationId}.provider/external-cache-path/
-->
<!--name自定义 .为SDCard/Android/data/应用包名/cache/-->
<external-cache-path
name="external-cache-path"
path="." />
<!--
自定义getExternalCacheDir()
文件保存路径为:/storage/emulated/0/Android/data/应用包名/cache/update/
Uri:content://${applicationId}.provider/external-cache-custom-path/
-->
<!--name自定义 update_file为SDCard/Android/data/应用包名/cache/update/和gettExternalCacheDirectory对应创建的文件夹保持一致-->
<external-cache-path
name="external-cache-custom-path"
path="update" />
<!--
代表 Environment.getExternalStorageDirectory()
文件保存路径为:/storage/emulated/0/updateFile/
Uri为:content://${applicationId}.provider/sdcard_root_externalStorageDirectory/
-->
<external-path
name="sdcard_root_externalStorageDirectory"
path="updateFile" />
<!--path要和代码中Environment.getExternalStorageDirectory(), "update" 保持一致否则异常
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/update/-->
</paths>
发起Install Apk意图,解决Android7.0共享文件URI转换,解决Android8.0未知来源apk安装
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri contentUri;
//解决Android7.0 共享文件uri转换
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", apkFile);
} else {
contentUri = Uri.fromFile(apkFile);
}
//解决Android8.0未知来源apk安装
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
if (contentUri != null) {
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
context.startActivity(intent);
}
未知来源权限请求回调
public void onActivityResult(int requestCode, int resultCode, Context context) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Log.e(TAG, "设置允许未知来源安装,重新发起安装");
} else {
if (requestCode == 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean hasInstallPermission = context.getPackageManager().canRequestPackageInstalls();
if (!hasInstallPermission) {
Log.e(TAG, "没有赋予 未知来源安装权限");
}
}
} else if (requestCode == 2) {
Log.e(TAG, "从安装页面回到欢迎页面--拒绝安装");
}
}
}