Start activity that is on the foreground - android

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.

Related

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 detect the state of an app other than mine?

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.

Android: test from a service, if an Activity is running.

I have a service that runs in background. I I wish it could perform an action, only if a certain activity is running. How can I test, from a service, if the activity is running?
You can use the Package Manager and the Activity Manager:
PackageManager: Class for retrieving various kinds of information related to the application packages that are currently installed on the device. You can find this class through getPackageManager().
ActivityManager: Interact with the overall activities running in the system.
But, a more simple approach would be: When starting your Activity (onCreate/onResume), send a message with service.send(..) or an Intent with service.onStartCommand(..) so the service knows that your particular Activity has been started.
Maybe you can do it the other way around, sending intent from your activity to the service.
For example, you can send an intent on the activity onResume, telling your service the activity is live, and another on onPause, telling the service the activity has stopped
Use the below method with your package name.It will return true if any of your activity is in foreground.
public boolean isForeground(String myPackage){
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > runningTaskInfo = am.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equals(myPackage)) return true;
return false;
}

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

Name of Activity or Receiver that calls the Service

I have a Intent service which is called from either a receiver or an activity. I would like to know the name of the receiver or activity that triggers the service. I don't want to use any extras or flags to pass to the service.
Is there a way to see the activity stack while the code is on the run?
As far as I know there is no way to detect sender of the Intent.
Is there a way to see the activity stack while the code is on the run?
You can use ActivityManager.RunningTaskInfo. Though it doesn't provide much API, it is probably sufficient for your requirements. Pseudocode:
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(10);
ActivityManager.RunningTaskInfo firstTask = runningTasks.get(0);
String topActivityName = firstTask.topActivity.getShortClassName();
String rootActivityName = firstTask.baseActivity.getShortClassName();
It gives you ability to retrieve the top and root activities of a specific task (AKA. back stack), Note that you need set persimmon GET_TASKS in AndroidManifest.xml.

Categories

Resources