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:
- values in views (like EditText) will be restored if your view have ids.
- your variable values I suggest you to save and restore with onSaveInstanceState() and onRestoreInstanceState().
Related Topics
Center Align Title in Action Bar Using Styles in Android
Garbage Collection Causes:Mediaplayer Finalized Without Being Released
Sdcard Content Exist But Cant See Them
Pass Arraylist Data into Soap Web Service in Android
Multiple Table SQLite Db Adapter(S) in Android
API to Automatically Upload APK to Google Play
Background Service Getting Killed in Android
Otp (Token) Should Be Automatically Read from the Message
Difference Between iPhone Simulator and Android Emulator
Android: Skimagedecoder:: Factory Returned Null
How to Detect App Removed from the Recent List
Picasso Loading of Image Spawned Inside Asynctask
Android Home Screen Widget (Icon, Label - Style)
Configuration on Demand Is Not Supported by the Current Version of the Android Gradle Plugin