ActivityManager.getRunningTasks is deprecated android
This should help you get started
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();
for (ActivityManager.AppTask task : tasks) {
Log.d(TAG, "stackId: " + task.getTaskInfo().stackId);
}
Android 5.0+ getRunningTasks is deprecated
So here is an update. Tested in 5.0 and 5.1.1 device. Working perfectly.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //For versions less than lollipop
ActivityManager am = ((ActivityManager) getSystemService(ACTIVITY_SERVICE));
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(5);
top = taskInfo.get(0).topActivity.getPackageName();
Log.v(TAG, "top app = " + top);
}else{ //For versions Lollipop and above
List<AndroidAppProcess> processes = ProcessManager.getRunningForegroundApps(getApplicationContext());
Collections.sort(processes, new ProcessManager.ProcessComparator());
for (int i = 0; i <=processes.size()-1 ; i++) {
if(processes.get(i).name.equalsIgnoreCase("com.google.android.gms")) { //always the package name above/below this package is the top app
if ((i+1)<=processes.size()-1) { //If processes.get(i+1) available, then that app is the top app
top = processes.get(i + 1).name;
} else if (i!=0) { //If the last package name is "com.google.android.gms" then the package name above this is the top app
top = processes.get(i - 1).name;
} else{
if (i == processes.size()-1) { //If only one package name available
top = processes.get(i).name;
}
}
Log.v(TAG, "top app = " + top);
}
}
}
Thanks to this library
Now i'm able to get foreground task in Android 5.0+
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 :)
Alternative to getRunningTasks in Android L
The system statistics api evoked in your question is also presented here and the detailed API is here. As far as I can see this API won't allow you to get the current top activity.
i.e. you have the ability to receive UsageEvent
with the type MOVE_TO_FOREGROUND
by using UsageStatsManager.queryEvents
but according javadoc :
The last few minutes of the event log will be truncated to prevent abuse by applications.
So it is really an API to get statistics, not to get realtime information. (note that the now deprecated getRunningTasks()
was neither intended to retrieve realtime data : javadoc)
On the other hand, if you are only interested in top activity of your own app: you can use getAppTasks()
returning a List<AppTask>
where you can get the RecentTaskInfo with getTaskInfo
EDIT : another approach is provided here.
It is based on UsageStats and UsageStats.getLastTimeUsed(). Drawback : it requires additional permission and gives you only the package name -not the activity-, but it gives you real time data.
How to get packagename of runningtask on Android M5.1.1
https://code.google.com/p/android-developer-preview/issues/detail?id=2347
Android suggests to use this for finding top application.
https://developer.android.com/reference/android/app/usage/UsageStatsManager.html
Related Topics
How to Disable "Next" Button on a Edittext Software Keyboard (Replace with "Done" Button)
How to Setup Multiple Alarms in Android
How to Debug on a Real Device (Using Eclipse/Adt)
Uninstall App Silently with System Privileges
How to Get Background Color from Current Theme Programmatically
Trying to Port Apache Poi to Android
Change Button Text and Action - Android Development
How to Enable Autostart Option for My App in Xiaomi Phone Security App Programmatically in Android
How to Prevent The Activity from Loading Twice on Pressing The Button
Android Studio: "Use Default Gradle Wrapper" VS. "Use Customizable Gradle Wrapper"
How to Make Space Between Linearlayout Children
Android Volley Imageloader - Bitmaplrucache Parameter
How to Pass a Message from Flutter to Native
How to Replace a String for a Buildvariant with Gradle in Android Studio
Android: Converting Color Image to Grayscale
How to Look-Up a Contact's Name from Their Phone Number on Android