Activity前后台切换
private void startWinewApp() {
String packageName = Settings.Global.getString(mContext.getContentResolver(), "FREATURE_AUTORUN_APK");
if (packageName != null && packageName.length() > 0) {
Intent launchIntent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
mContext.startActivity(launchIntent);
}
}
这种更好:
private void startWinewApp() {
final ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> recentList = am.getRunningTasks(30);
for (ActivityManager.RunningTaskInfo info : recentList) {
if (info.topActivity.getPackageName().equals(WINEW_APP_PACKAGE_NAME)) {
Log.d(TAG, "startWinewApp taskId="+info.id);
am.moveTaskToFront(info.id, ActivityManager.MOVE_TASK_WITH_HOME);
return;
}
}
Intent launchIntent = mContext.getPackageManager().getLaunchIntentForPackage(WINEW_APP_PACKAGE_NAME);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
| Intent.FLAG_ACTIVITY_NEW_TASK);
try {
mContext.startActivity(launchIntent);
} catch (ActivityNotFoundException e) {
Log.w(TAG, "Unable to launch recent winew", e);
}
}
将当前的Activity放到后台:
private ActivityManager.RunningTaskInfo getTopTaskInfo() {
ActivityManager activityManager = (ActivityManager) (mContext
.getSystemService(Context.ACTIVITY_SERVICE));
List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager
.getRunningTasks(1);
if (runningTaskInfos != null) {
return runningTaskInfos.get(0);
}
return null;
}
try {
int taskId = getTopTaskInfo().id;
ActivityManagerNative.getDefault().moveTaskToBack(taskId);
}catch (RemoteException e) {
}