How to notify when thirdparty application gets called - android

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

Related

Android: How to know what application is opened to foreground and do something when it goes to background

The title is bad, sorry. Could figure out how to say this in short.
So, I'm developing an application for Android which will only be used in my own couple tablets which are going to public use.
I need a service running in the background (or so I currently believe, prove me wrong if possible) which keeps track which application is foreground, and when that application goes to background the service starts activity which will go foreground and show something to the user(for example like some ad, or some review window which asks for start rating).
How could this be done?
Thank you.
E: The other applications can be 3rd party, so I can't modify the code of those.
When u start other activity at that time you current activity is OnPause() state and other activity goes onCreate() State.
You Must refer Activity LifeCycle for this.
You have to use Accessibility service. Starting from Android Lollipop you can't gather this kind of information. The only way is via accessibility docs.
to check other running tasks you should use ActivityManager.
ActivityManager acm = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> Ru_tasks = acm.getRunningTasks(NUMBER_OF_TASKS_U_WANT);
if (!Ru_tasks .isEmpty()) {
LOOP FOR EACH TASK (i is counter)
ComponentName topActivity = tasks.get(i).topActivity; // Or replace .topactivity with task name
if (topActivity.getPackageName().equals(WHAT_YOU LOOK FOR)) {
//Do WHAT YOU WANT!
}
}
get GET_TASKS permission

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.

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--------------");

Access Started Service after Activiy is destroyed and Re-created

I've created an Started Service from an activity. I'm curious about: what happens if the activity gets destroyed? Does the service still run? (I think it runs!)
Then when the activity is re-created, is there any way to communicate with that service? I just need to know whether the service is still running, if running, then I want to pass a reference of the activity to the service.
Okay, a comment isn't big enough. Here's some code I use to start a service. Following the start service is a function I use to determine if the service is running. The class DownloadDatabase extends IntentService. The service downloads a 70 megabyte SQLite database that was created externally to Android.
Intent intentService = new Intent(this, DownloadDatabase.class);
startService(intentService);
The following function checks to see if the service is already running. It is coded as a function because I call it from other routines that do other verifications if is not running.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager
.getRunningServices(Integer.MAX_VALUE)) {
if ("com.hodsonssoftwarellc.nutritionexplorer.DownloadDatabase"
.equals(service.service.getClassName())) {
return true;
}
}
return false;
}
You will also need the following permission in your manifest:
uses-permission android:name="android.permission.GET_TASKS"
What is happening in the isMyServiceRunning function is as follows:
The Activity Manager is acquired.
A list of tasks that are currently running is retrieved and used as the argument in the special case for loop.
As each task is retrieved from the list, the task name is compared to my fully qualified class name for the download service.
If a match is found, the function returns true. Otherwise it returns false.
So, in answer to your question, yes, you can check to see if it running from your task that starts it. Even if you exit the task and start it again. You can also check if is running from a different activity or app.
Note: This code was written for 2.x compatibility. If you are doing a download in version 3 or higher, use the download manager instead of writing your own download service.

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