解决了跳转到其他应用的各种问题。先上代码。
private Intent getAppIntent(String packageName) {
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);
String mainAct = null;
List<ResolveInfo> list = mPackageManager.queryIntentActivities(intent,PackageManager.MATCH_ALL);
int count = list.size();
for(int i = 0;i<count;i++){
ResolveInfo rinfo = list.get(i);
if (rinfo.activityInfo.packageName.equals(packageName)) {
mainAct = rinfo.activityInfo.name;
break;
}
}
if (TextUtils.isEmpty(mainAct)) {
return null;
}
intent.setComponent(new ComponentName(packageName, mainAct));
long serialNumber = -1;
//17
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
UserManager mUserManager = (UserManager)getSystemService(Context.USER_SERVICE);
mUserManager.getSerialNumberForUser(android.os.Process.myUserHandle());
}
intent.putExtra("profile", serialNumber);
return intent;
}
该方法主要参照了Launcher3中的代码,同时也参考了一些网友的代码。
launcher3的源代码如下:
public static Intent makeLaunchIntent(Context context, LauncherActivityInfoCompat info,
UserHandleCompat user) {
long serialNumber = UserManagerCompat.getInstance(context).getSerialNumberForUser(user);
return new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_LAUNCHER)
.setComponent(info.getComponentName())
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
.putExtra(EXTRA_PROFILE, serialNumber);
}
/**
* Intent extra to store the profile. Format: UserHandle
*/
static final String EXTRA_PROFILE = "profile";
若有错误请多多指教!