Getting current running application - android

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

Related

How to get package name of any app when it gets opened by user?

I need to get package name or app name of any app when it gets opened by user. I searched about this but didn't get proper example.
Anyone can give me suggestions for this case?
Thank you so much.!
You have to create your background service for that , which will continuously monitor top app in device .
So first of all make a service which will start after your app launch .In your service , use this
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
activityOnTop=ar.topActivity.getClassName();
activityOnTop will give you the current running app in your phone . Now do whatever you want after getting top activity.

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 know Package Name of a Top running app from my service running in background (android)

How can i know the package name of top Running application.
https://www.dropbox.com/s/2p4iuj7dj7zv5u4/Screenshot%20%28234%29.png
(in the link , above application is temple run 2 and package is com.imangi.templerun2 )
I need to retrieve the package name(ie com.imangi.templerun2) from my service and store it in String named packageName .
How can i do it ???
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// Return a list of the tasks that are currently running,
// with the most recent being first and older ones after in order.
// Taken 1 inside getRunningTasks method means want to take only
// top activity from stack and forgot the olders.
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName(); // here it is

How do I listen to an app that is deactivated from another app - Android

I wanna make an app that listens another app's deactivating.
For example my app will be like:
listening Facebook app...
Facebook app is activated.
Facebook app is deactivated. (that one which i want to catch.)
How can i do that?
Thanks in advance.
If by "activating" and "deactivating", you mean "moving to the foreground (resuming)" and "moving to the background (pausing)", there's no appropriate way to do what you're asking, since no broadcasts are fired when apps launch. You can use ActivityManager preferably in a Service like follows:
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for(RunningAppProcessInfo appProcess : appProcesses){
if(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
Log.i("Foreground App", appProcess.processName);
}
}
However, this method will take a toll on the device's resources and is not recommended.

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.

Categories

Resources