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
I was recently checking on available services which also was using the activity manager. Probably just change this to the getRecentTasks(int maxNum, int flags) method and modify the below to your needs!?
private boolean isServiceOnline() {
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
for (int i = 0; i < services.size(); i++) {
String packageClassName = services.get(i).service.getClassName();
if (packageClassName.equals("org.couchdb.android.CouchService")) {
Log.d(TAG, "Service Nr. " + i + " :" + services.get(i).service);
Log.d(TAG, "Service Nr. " + i + " package name : " + services.get(i).service.getPackageName());
Log.d(TAG, "Service Nr. " + i + " class name : " + packageClassName);
;
return true;
}
}
return false;
}
int numberOfTasks = 1;
ActivityManager m = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
//Get some number of running tasks and grab the first one. getRunningTasks returns newest to oldest
RunningTaskInfo task = m.getRunningTasks(numberOfTasks).get(0);
//Build output
String output = "the last application you've executed is '"+task.id+"' and the PID is '"+task.baseActivity.toShortString()+"'";
getRunningTasks
RunningTaskInfo
Here is my solution:
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> 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.