How to check foreground Android app activity - android

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"/>

Related

find whether extras are passed to current running activity from a service

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 to continuously monitor any activity that comes into foreground

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.

How to know Package Name of a Top running app from my service running in background (android)

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

Start activity that is on the foreground

Is there a way to know if the activity I want to start, is already the activity on the foreground?
You can get the name of Activity in foreground with ActivityManager. Here is the code:
ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
String foreGroundActivityName = runningTaskInfos.get(0).topActivity.getClassName().toString();
You can then compare the name with your Activity. And make sure you add android.permission.GET_TASKS permission in manifest file.

Getting current running application

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" />

Categories

Resources