如题,Android App新版本检测升级
实现步骤:
1、权限
小米11,android12,必须申请android.permission.REQUEST_INSTALL_PACKAGES权限
<!-- 文件读写权限 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2、provide
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.itcast.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
3、filepaths
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/com.itcast.app/" name="files_root"/>
<external-path path="." name="external_storage_root"/>
</paths>
4、下载完之后,调用函数
/**
* 安装apk
*/
private void installApk() {
File apkfile = new File(m_strApkFilePath);
if (!apkfile.exists()) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//兼容android7.0以上版本
Uri uri = Uri.fromFile(apkfile);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//通过FileProvider创建一个content类型的Uri
uri = FileProvider.getUriForFile(m_context, "com.itcast.fileprovider", apkfile);
// 给目标应用一个临时授权
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
m_context.startActivity(intent);
}