How to detect the state of an app other than mine? - android

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.

Related

Close the service from the library only when the entire app goes to the background

I searched a lot in the net about it, the solutions are based on onPause and onDestroy. I want to give a library to a developer who just needs to paste a few lines of code from the library in his app which will enable the developer to create a service and destroy it when his entire app is in the background.
Does the Android OS send some kind of signals or intents when the app or the activity is changed (other than onPause or onStop method), so that i can catch that in a broadcast receiver from my library and do some actions.
If you know the package name of your app, try this (put this following snippet in the onCreate method of your app):
ActivityManager am= (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
Then,
boolean exit = false;
while(!exit)
{
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(!componentInfo.getPackageName().equals("Your package name"))
{
//Do your work here
exit = true;
}
}
When you start your app, this will be put into componentInfo. The taskInfo.get(0).topActivity will return the activity in the foreground. Hence you can know that your app has gone to the background by comparing package using the second code snippet.
Note:Put this second code snippet in an Asynctask so that the checking of whether the app has started can be done in the background.

Receiver for Android App info activity

Is there any way to create a BroadcastReceiver that can be fired when user goes to App info activity (Settings > Application Management > Pick an app)? If so, will we know which app's info is showing up?
Thanks,
hmlaken
There is no broadcast which is fired when user opens a particular activity.
If you want to know the activity which the user is currently on, you can try to periodically check the activity stack to know which activity the user is currently viewing.
while(true) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runTaskInfo = am.getRunningTasks(1);
String pkg = runTaskInfo.get(0).topActivity.getPackageName();
String activity = runTaskInfo.get(0).topActivity.getClassName();
String tmp = pkg + " : " + activity;
Thread.sleep(1000);
}
Run this in a thread and it will wake up every 1 second and give you the activity which is currently on the screen. Compare it with activity you are looking for to know when the user calls it.
No, there is no Intent broadcasted.
(Also why do you want this information?)
If you want to know when an user removes your app: listen to the PACKAGE_REMOVED intent.

Determining if an Activity exists on the current device

I want to know what activity is being displayed in android device
if the activity that I will search I execute code
if not I execute another code
purpose is to make an android service to check this task
This should work for you:
Get active Application name in Android
Just tweak it with the functionality you want, and you should be able to check rather easily.
Are you sure that's what you want?
purpose is to make an android service to check this task
If you told us what was the reason behind your "purpose", I'm almost sure we could help you with a better way of doing what you want (probably we could find a way which would waste less battery and which would antagonize less your users).
In any case, here is the answer to your question:
Use getRunningTasks of ActivityManager to get a list of the recently launched activities.
public
List
getRunningTasks (int maxNum)
Return a list of the tasks that are
currently running, with the most
recent being first and older ones
after in order. Note that "running"
does not mean any of the task's code
is currently loaded or activity -- the
task may have been frozen by the
system, so that it can be restarted in
its previous state when next brought
to the foreground.
You might also want to use the flag RECENT_WITH_EXCLUDED to make sure you get all the activities, even the ones that purposefully exclude themselves from that list.
public static final int RECENT_WITH_EXCLUDED
Added in API level 1 Flag for use with getRecentTasks(int, int):
return all tasks, even those that have set their
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS flag.
I found this code it works very well
Log.i("---------------", "----------onCreate--------------");
Log.i("-----test------", "----------1--------------");
ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
Log.i("-----test------", "----------2-------------");
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.i("-----test------", "---------3--------------");
ComponentName componentInfo = taskInfo.get(0).topActivity;
Log.i("-----test------", "----------4--------------");
Log.i("------------------", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName()+" Package Name : "+componentInfo.getPackageName());
Log.i("-----test------", "----------5--------------");

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;
}

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