See Android recent task executed by the user

后端 未结 3 1265
广开言路
广开言路 2020-11-30 08:39

I would like to watch the recent task of my android phone. I was trying some code from internet but non of them work properly. I just want to get the PID and Name of the las

3条回答
  •  有刺的猬
    2020-11-30 08:56

    Here is my solution:

    final   ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final List recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
    
        for (int i = 0; i < recentTasks.size(); i++) 
        {
            Log.d("Executed app", "Application executed : " +recentTasks.get(i).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(i).id+"");         
        }
    

    Be careful!!The last ID is not the PID of the process!! If you want to get the PID of the process I used the following command :

    mLogcatProc = Runtime.getRuntime().exec(new String[] {"ps"}); 
    

    Read the result finding the application name and then split to obtain PID process.

提交回复
热议问题