Name of Activity or Receiver that calls the Service - android

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.

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.

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

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.

Categories

Resources