/**
* 兼容8.0的安装apk
* @param context
* @param filePath
*/
public static void installApkComp(Context context,String filePath) throws FileNotFoundException
,NullPointerException{
if (filePath == null || "".equals(filePath)) {
throw new NullPointerException("路径字符是null");
}
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("文件不存在");
}
if (Build.VERSION.SDK_INT >= 26) {
boolean b = context.getPackageManager().canRequestPackageInstalls();
if (b) {
installApk(context,filePath);
//安装应用的逻辑(写自己的就可以)
} else {
//设置安装未知应用来源的权限
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
} else {
installApk(context,filePath);
}
}
/**
* 安装apk,私有目录
* @param mContext
* @param filePath
*/
public static void installApk(Context mContext,String filePath){
File apkFile = new File(filePath);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.w(TAG, "版本大于 N ,开始使用 fileProvider 进行安装");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(
mContext
, mContext.getPackageName()+".fileprovider"
, apkFile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
}
mContext.startActivity(intent);
}
网友评论