I want my application to continuously listen to any activity that comes in foreground. I have a code to know which activity is in foreground, but my question is how do i continuously monitor in my application
Code :
ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
Log.d(WebServiceHelper.TAG, "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName()+" Package Name : "+componentInfo.getPackageName());
Thanks
You should implement using BroadcastReceiver if Android provides any such kind of notification on task/activity change, else you will need to launch a Service which checks the current activity/task after some duration you set.
Related
i'm currently setting up an automation on my Android device and i need to know which activity is currently in the foreground so i can include it in the automation as a trigger.
I'm looking for the app activity withing the whole package
Thanks
Sure, use Activity Manager.
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
You will need the following permission on your manifest:
<uses-permission android:name="android.permission.GET_TASKS"/>
Im building an app in which i need to know the current running activity from a service. I was able to achieve this with the below code found in stack overflow:
ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
Toast.makeText(this, "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName(), Toast.LENGTH_LONG);
Now i would want to know if any extras are passed to this current running activity intent or not. Also if the extras were passed i would want to know the values assuming that i have the key for the values?
How can i know the package name of top Running application.
https://www.dropbox.com/s/2p4iuj7dj7zv5u4/Screenshot%20%28234%29.png
(in the link , above application is temple run 2 and package is com.imangi.templerun2 )
I need to retrieve the package name(ie com.imangi.templerun2) from my service and store it in String named packageName .
How can i do it ???
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// Return a list of the tasks that are currently running,
// with the most recent being first and older ones after in order.
// Taken 1 inside getRunningTasks method means want to take only
// top activity from stack and forgot the olders.
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName(); // here it is
I have looked around, but all suggestions are for checking state of my own app/activity.
using the Life Cycle.
I need to check if another app is in foreground/ running in background / paused .
Is it possible. Based on the state of the app i need to change my further actions.
ActivityManager acm=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = acm.getRunningTasks(1);
taskInfo.get(0).topActivity.getClassName();
ComponentName componentInfo = taskInfo.get(0).topActivity;
String packageName=componentInfo.getPackageName();
I used Activity manager to find the top package / activity if this matches the requrements i send a broadcast to the app that will update the info from the db. This can simply be done by registering a broadcast receiver using code in the onResume and Unregister it in OnPause.
So checking the Top package has just become a double check.
How may I get the current running application?
I want to check if the application is running or not in service.
I used the following code for that.
ActivityManager activityManager =
(ActivityManager)
this.getSystemService(ACTIVITY_SERVICE );
PackageManager pm =getPackageManager();
procInfos = activityManager.getRunningAppProcesses();
But it is returning me list of several applications, even if I pressed back button on their home screen. The running app list is still showing them in the list. But I want that list to show only the apps in foreground.
So, how to differentiate between foreground and background running apps?
Or in other words, how to detect current foreground running applications, or which are not destroyed. I don't want to get background running apps.
You can try this code it gives the package of the currently active screen:
ActivityManager am = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(!componentInfo.getPackageName().equals("your.package.name"))
{
//Do your stuff
}
You might need the permission
<uses-permission android:name="android.permission.GET_TASKS" />