Receiver for Android App info activity - android

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.

Related

Notification on Onfinish() function of a CountDownTimer class

I am trying to build a Count down timer app. where i need to notify user when Coundown ends to 0. i use CountDownTimer class to Count.
Here is what i wanted:
when the countdown hit 0 a notification will appear on status bar with notification tune with vibration(lets say 200ms) and when they press notification it goes the app page where they left last time(I dont want open any Activity). also if user still on the app and notification comes on status bar, When they press it,it goes right where they are(Dont want start any activity).
Here is what i already tried:
i tried notification on onFinish() function of CountDownTimer class. it work just fine but when user press the notification it just start new Activity instead of the current app page.
Its been 2 days i search for the solution still no working solution.
so Any help would be appreciated! Thanks!
Check if the application is running in background or not and either push the new activity (in background scenario) or leave it as it is (as user is already in app)
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
ActivityManager.RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
PackageManager pm = context.getPackageManager();
PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0);
String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString();
if(!"Your App name".equalsIgnoreCase(foregroundTaskAppName) ){
isInBackground=false;
}
App name should be the one in strings.xml
<string name="app_name">Your app name</string>

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.

How to notify when thirdparty application gets called

When third party apps in mobiles gets called i would like to notify user in a text message ..I am creating an app that will notify the user when i tries to open an application .
Help me to receive intent when ever app is tries to open..else sugges me some steps to find when the app icon is being clicked..
you can start a service, and execute every single minutes. Use ActivityManager to get the top activity in current running task. Something like this:
ActivityManager mActivityManager = (ActivityManager) context.getSystemService("activity");
ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;
And then, you can do anything you want. Hope useful to u.
Don't forget the permission:
<uses-permission android:name="android.permission.GET_TASKS"/>

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.

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