See Android Recent Task Executed by the User

See Android recent task executed by the user

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.

How to get all the tasks that are running currently

Use getRunningAppProcesses, which will return a list of RunningAppProcessInfo objects that will give you some data about each running app. One of the pieces of information you can retrieve about the app is its importance, which can have a value IMPORTANCE_FOREGROUND which indicates that the process is the currently active application!

Related Documentation:

getRunningAppProcesses

RunningAppProcessInfo

IMPORTANCE_FOREGROUND

If this helped you, feel free to press that arrow to the left :)

How to find the currently running applications programmatically in Android?

ActivityManager activity_manager = (ActivityManager) context
.getSystemService(Activity.ACTIVITY_SERVICE);

ActivityManager has method getRunningTasks(int). ActivityManager seems to be the solution you are searching for.

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+"");
}

Also, have a look at following thread:
See Android recent task executed by the user

What happens when already destroyed activity by OS is revoked by user from recent apps?

According to LifeCycle of an activity, after your app will be killed by the android OS (to get memory for more prioritized apps), your activity will start from onCreate() method, and go through the cycle, as usual) What about your data:

  1. values in views (like EditText) will be restored if your view have ids.
  2. your variable values I suggest you to save and restore with onSaveInstanceState() and onRestoreInstanceState().


Related Topics



Leave a reply



Submit