安装本地apk文件,会报exposed beyond app through Intent.getData()异常,但是用了
https://blog.csdn.net/weixin_42415553/article/details/96440013?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165482831316780357288214%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=165482831316780357288214&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-96440013-null-null.142^v12^pc_search_result_cache,157^v13^control&utm_term=exposed+beyond+app+through+Intent.getData%28%29&spm=1018.2226.3001.4187
这个方法后,不报异常,但是无法打开apk文件,没有任何响应
先试试手动装一下,是否能打开
下面是我封装好的,常用方法(包含判断某个应用是否安装、安装应用、打开应用等),拿走不谢
/**
* 安装apk
*
* @param context
* @param file
*/
public static void installApk(Context context, File file) {
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//判断是否是AndroidN以及更高的版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(uri, "application/vnd.android.package-archive");
}
context.startActivity(intent);
}
/**
* 卸载apk
*
* @param context 上下文
* @param packageName 包名
*/
public static void uninstallApk(Context context, String packageName) {
Intent intent = new Intent(Intent.ACTION_DELETE);
Uri packageURI = Uri.parse("package:" + packageName);
intent.setData(packageURI);
context.startActivity(intent);
}
/**
* 某应用是否安装
*
* @param context
* @param packageName
* @return
*/
public static boolean isApkInstalled(Context context, String packageName) {
if (TextUtils.isEmpty(packageName))
return false;
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(
packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
public static boolean isApkInstalled(Context context, String packageName, String verName) {
if (TextUtils.isEmpty(packageName))
return false;
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(
packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
String verNameNow = getVerName(context, packageName);
if (verName.equals(verNameNow))
return true;
else
return false;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
/**
* 打开另一个应用
*
* @param activity
* @param packageName
* @param className
* @param errorHint
*/
public static void showAnotherApp(Activity activity, String packageName,
String className, String errorHint) {
LogUtils.d(TAG, "showAnotherApp");
if (AppUtils.isApkInstalled(activity, packageName)) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
activity.startActivity(intent);
} else {
ToastUtils.showShortToast(errorHint + "未安装");
}
}
/**
* 根据包名打开应用
*
* @param activity
* @param packagename
*/
public static void showAnotherAppWithPackageName(Activity activity, String packagename, String errorHint) {
if (AppUtils.isApkInstalled(activity, packagename)) {
// 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
PackageInfo packageinfo = null;
try {
packageinfo = activity.getPackageManager().getPackageInfo(packagename, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageinfo == null) {
return;
}
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
//resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(packageinfo.packageName);
// 通过getPackageManager()的queryIntentActivities方法遍历
List<ResolveInfo> resolveinfoList = activity.getPackageManager()
.queryIntentActivities(resolveIntent, 0);
if (resolveinfoList == null || resolveinfoList.size() == 0) {
return;
}
ResolveInfo resolveinfo = resolveinfoList.iterator().next();
if (resolveinfo != null) {
// packagename = 参数packname
String packageName = resolveinfo.activityInfo.packageName;
// 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packagename.mainActivityname]
String className = resolveinfo.activityInfo.name;
// LAUNCHER Intent
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// 设置ComponentName参数1:packagename参数2:MainActivity路径
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
activity.startActivity(intent);
}
} else {
ToastUtils.showShortToast(errorHint + "未安装");
}
}