/**
* 判断是否是8.0需要处理未知应用来源权限问题,否则直接安装
* @param apkPath
*/
private void checkIsAndroidO(String apkPath) {
if (Build.VERSION.SDK_INT >=26) {
boolean b = getPackageManager().canRequestPackageInstalls();
if (b) {
InstallUtil.installApk(this, apkPath);
//安装应用的逻辑(写自己的就可以)
}else {
//设置安装未知应用来源的权限
Intent intent =new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
startActivityForResult(intent, 10012);
}
}else {
InstallUtil.installApk(this, apkPath);
}
}
InstallUtil.installApk 方法:
public class InstallUtil {
public static void installApk(Context context, String fileName){
File file =new File(fileName);
int index = fileName.lastIndexOf(".");
String nameExtra = fileName.substring(index +1, fileName.length());
if (nameExtra.equals("apk")) {
Intent intent =new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >=7) {//20200616 android10以上版本安装没有权限报错问题解决
Uri apkUri = FileProvider.getUriForFile(context, "com.example.finance_guigu.fileprovider", file); //与manifest中定义的provider中的authorities保持一致
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
}else {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
context.startActivity(intent);
}
}
}