Android app 运行时按了Home键,进入到另一程序,此程序中再调用原来程序,回到原来页面的方法:
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName appTopActivity = null;
List infos = manager.getRunningTasks(100);//获取当前正在运行的应用列表
for (RunningTaskInfo info : infos) {
System.out.println("topActivity:" + info.topActivity.getPackageName() + " baseActivity:" + info.baseActivity.getPackageName());
//判断原app是否还在运行
if (info.topActivity.getPackageName().equals(appName)
&& info.baseActivity.getPackageName().equals(appName)) {
appTopActivity = info.topActivity;
}
}
if(appTopActivity != null){
Intent in = new Intent();
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //在receiver或者service里新建activity都要添加这个属性,
in.setComponent(appTopActivity);
//使用addFlags,而不是setFlags
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //清除掉Task栈需要显示Activity之上的其他activity
in.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); //加上这个才不会新建立一个Activity,而是显示旧的
context.startActivity(in);
}