Determining the current foreground application from a background task or service

后端 未结 13 1427
我寻月下人不归
我寻月下人不归 2020-11-22 02:29

I wish to have one application that runs in the background, which knows when any of the built-in applications (messaging, contacts, etc) is running.

So my questions

13条回答
  •  终归单人心
    2020-11-22 02:49

    Do something like this:

    int showLimit = 20;
    
    /* Get all Tasks available (with limit set). */
    ActivityManager mgr = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List allTasks = mgr.getRunningTasks(showLimit);
    /* Loop through all tasks returned. */
    for (ActivityManager.RunningTaskInfo aTask : allTasks) 
    {                  
        Log.i("MyApp", "Task: " + aTask.baseActivity.getClassName()); 
        if (aTask.baseActivity.getClassName().equals("com.android.email.activity.MessageList")) 
            running=true;
    }
    

提交回复
热议问题