Determining the current foreground application from a background task or service

后端 未结 13 1450
我寻月下人不归
我寻月下人不归 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:52

    In lollipop and up:

    Add to mainfest:

    
    

    And do something like this:

    if( mTaskId < 0 )
    {
        List tasks = mActivityManager.getAppTasks(); 
        if( tasks.size() > 0 )
            mTaskId = tasks.get( 0 ).getTaskInfo().id;
    }
    

提交回复
热议问题