在android10的环境下 发现部分华为手机不能正常更新安装,检查了一下爆出异常
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file://storage/emulated/0/文件夹名/文件名.apk typ=application/vnd.android.package-archive }
解决方法:
1.mainifest文件下添加:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="包名.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
2.xml文件:
<resources>
<paths>
<external-path path="." name="my_files" />
</paths>
</resources>
3.安装添加版本判断
private void installAPK() {
File apkFile = new File(saveFilePath);
if (!apkFile.exists()) {
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { //10.0+以上版本
Uri apkUri = FileProvider.getUriForFile(context, "包名.fileProvider", apkFile); //与manifest中定义的provider中的authorities="包名.fileprovider"保持一致
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
i.setDataAndType(Uri.parse("file://" + apkFile.toString()),
"application/vnd.android.package-archive");
}
context.startActivity(i);
}