当我从我自己的应用程序中打开已安装在手机中的应用程序时,我遇到了这个问题。
权限拒绝:启动Intent {act = android.intent.action.MAIN 猫= [android.intent.category.LAUNCHER] 来自的cmp = com.qq.reader / .widget.picbrowser.PictureActivity} ProcessRecord {3823559b 16851:com.pax.market.android.app/u0a322} (pid = 16851,uid = 10322)未从uid导出 10324
public static void openApp(Activity activity,String packageName) {
PackageInfo pi;
try {
pi = activity.getPackageManager().getPackageInfo(packageName, 0);
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.setPackage(pi.packageName);
PackageManager pManager = activity.getPackageManager();
List apps = pManager.queryIntentActivities(
resolveIntent, 0);
ResolveInfo ri = (ResolveInfo) apps.iterator().next();
if (ri != null) {
packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
activity.startActivity(intent);
}
} catch (NameNotFoundException e) {
Log.e(TAG, "Package Name not found: " + e.getMessage() + ", App is not installed.");
} catch (SecurityException e){
SnackBar.newBuilder().activity(activity).
text(activity.getString(R.string.permission_denied_to_open))
.build().show();
}
}
`
答案 0 :(得分:2)
列出apps = pManager.queryIntentActivities(resolveIntent,0); 将会找到多个活动。
例如:appName:QQReader,packagename:com.qq.reader。
你会发现两个活动:
com.qq.reader.widget.picbrowser.PictureActivity
com.qq.reader.activity.SplashActivity
无法启动PictureActivity,因为它的导出= false,可以启动SplashActivity
代码修改如下:
public static void openApp(Activity activity,String packageName) {
PackageInfo pi;
try {
pi = activity.getPackageManager().getPackageInfo(packageName, 0);
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.setPackage(pi.packageName);
PackageManager pManager = activity.getPackageManager();
List<ResolveInfo> apps = pManager.queryIntentActivities(
resolveIntent, 0);
for(ResolveInfo app: apps){
Log.d(TAG,String.format("%s %s",app.activityInfo.packageName,app.activityInfo.name));
packageName = app.activityInfo.packageName;
String className = app.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
try {
activity.startActivity(intent);
}catch(SecurityException e){
e.printStackTrace();
}
}
/* ResolveInfo ri = (ResolveInfo) apps.iterator().next();
if (ri != null) {
packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
activity.startActivity(intent);
}*/
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Package Name not found: " + e.getMessage() + ", App is not installed.");
} catch (SecurityException e){
SnackBar.newBuilder().activity(activity).
text(activity.getString(R.string.permission_denied_to_open))
.build().show();
}
}