Resume application and stack from notification

后端 未结 8 1523
一向
一向 2020-11-22 16:04

I want to resume my app from a status bar notification in the exact same manner as when the user taps its icon in the launcher.

That is: I want the stack to be in th

相关标签:
8条回答
  • 2020-11-22 17:04
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.REORDER_TASKS" />
    
    private void bringApplicationToForeground(){
        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    
          List<ActivityManager.AppTask> tasksList = am.getAppTasks();
          for (ActivityManager.AppTask task : tasksList){
            task.moveToFront();
          }
        }else{
    
          List<ActivityManager.RunningTaskInfo> tasksList = am.getRunningTasks(Integer.MAX_VALUE);
          if(!tasksList.isEmpty()){
            int nSize = tasksList.size();
            for(int i = 0; i < nSize;  i++){
              if(tasksList.get(i).topActivity.getPackageName().equals(getPackageName())){
                am.moveTaskToFront(tasksList.get(i).id, 0);
              }
            }
          }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 17:10

    I also had the same problem and try to resolve the problem like @GrAnd's answer:

    final Intent notificationIntent = new Intent(context,YourActivity.class);
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    

    This works, no doubts about it but the problem is when you set your intent as ACTION_MAIN. Then you will not be able to set any bundle to the intent. I mean, your primitive data will not be received from the target activity because ACTION_MAIN can not contain any extra data.

    Instead of this, you can just set your activities as singleTask and call your intent normally without setting ACTION_MAIN and receive the intent from onNewIntent() method of your target activity.

    But beaware if you call, super.onNewIntent(intent); then a second instance of the activity will be created.

    0 讨论(0)
提交回复
热议问题