//获取ActivityManager ActivityManager mAm = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); //获得当前运行的task List<ActivityManager.RunningTaskInfo> taskList = mAm.getRunningTasks(100); for (ActivityManager.RunningTaskInfo rti : taskList) { //找到当前应用的task,并启动task的栈顶activity,达到程序切换到前台 if(rti.topActivity.getPackageName().equals(context.getPackageName())) { //判断app进程是否存活 Log.i("NotificationReceiver", "the app process is alive"); try { Intent resultIntent = new Intent(context, Class.forName(rti.topActivity.getClassName())); resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); context.startActivity(resultIntent); }catch (ClassNotFoundException e) { e.printStackTrace(); } return; } } //若没有找到运行的task,用户结束了task或被系统释放,则重新启动mainactivity Intent intent = new Intent(); intent.setClass(context.getApplicationContext(), LoadingActivity.class); intent.setAction(Intent.ACTION_MAIN); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.getApplicationContext().startActivity(intent);
android 按Home键退出后点击通知回到应用
最新推荐文章于 2024-06-03 20:45:00 发布