decide if app is runnig or is not - android

I have an application in which I have a particular event receiver. My goal is to display a dialog box when the application is running, otherwise (ie if the application is not running) show a notification.
The events such as notifications or displaying dialog boxes I have implemented. I can not determine whether the user has the application on or off.
Any ideas ?

ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfoList = am.getRunningTasks(1);
ComponentName componentName = runningTaskInfoList.get(0).topActivity;
String runningActivityName = componentName.getClassName();
String runningActivityPackageName = componentName.getPackageName();
It requires "android.permission.GET_TASKS" permission.

Exact here -
ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);
if((serviceList.size() > 0)) {
boolean found = false;
for(int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
//Your service or activity is running
found = true;
break;
}
}
if(found) {
//Close your app or service
}
}
Source - how-can-i-check-if-my-app-is-running.

application still alive or not
public boolean isAppRunning()
{
boolean appFound = false;
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo recentTask : recentTasks)
{
if (recentTask.baseActivity.getPackageName().equals("package.Name"))
{
appFound = true;
break;
}
}
return appFound;
}
need permission also
<uses-permission android:name="android.permission.GET_TASKS" />
Please check one my question is related to your question. in that please check comments.
Check application is minimized/background

Related

Is there any way to detect whether the installed application is in use or not?

I know we can detect the installed application but is there any way to detect that which application is in use or which is in idle/closed state?
For example, if I open the Whatsapp, it can detect the WhatsApp and Toast me a message "using WhatsApp".
You cannot detect an App launch in Android, but you can get the list of currently open apps and check if the app you're looking for is open or not using the following code:
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for")) {
// Do your stuff here.
}
}
You can also check if the app is running in the foreground using this method
public static boolean isForeground(Context ctx, String myPackage){
ActivityManager manager = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equals(myPackage)) {
return true;
}
return false;
}
this response is from this post :
How to get the list of running applications?

Android - How to check my app is open when receive a push-notification?

I need best solution. When I received a push notification, I want to know if my app is open or not, because if my application is open when the user onClick the notification I want to call specific method, but if the application is close I want to open the application.
Basically what you need is way to check if one of your activity is in Foreground. May be you should check this How to determine if one of my activities is in the foreground
//Use this method to check your app is running or not
public boolean isAppRunning(){
String packageName="Your package name";
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningAppProcessInfo> procInfoslist = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfoslist.size(); i++)
{
if(procInfoslist.get(i).processName.equals(packageName))
{
return true;
}
}
return false;
}
`
//Use this method to check any activity is running or not
public boolean isActivityRunning(Context ctx) {
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks) {
if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}

How to check if an activity is running in background/foreground from a service?

I have created a service which starts from an activity's onCreate and stops on the activity's onDestroy method. Now I have to check from a method of the service that whether the activity is running in foreground/background(for some cases like application is forcefully closed). How can I do that?
I need to do this coz as far I know there is no guarantee of calling onDestroy method of an activity if any the application is forcefully closed or any kind of crash. So, my service which starts when my activity launches won't stop after any crash or any forceful closing event.
I have seen this link where foreground activities can be checked. But I need to check a running activity in whatever state (either foreground or background)
Final Update
To check for all activities:
#Override
protected void onStop() {
super.onStop();
if (AppConstants.isAppSentToBackground(getApplicationContext())) {
// Do what ever you want after app close simply Close session
}
}
Method to check our app is running or not:
public static boolean isAppSentToBackground(final Context context) {
try {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
// The first in the list of RunningTasks is always the foreground
// task.
RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
String foregroundTaskPackageName = foregroundTaskInfo.topActivity
.getPackageName();// get the top fore ground activity
PackageManager pm = context.getPackageManager();
PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(
foregroundTaskPackageName, 0);
String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo
.loadLabel(pm).toString();
// Log.e("", foregroundTaskAppName +"----------"+
// foregroundTaskPackageName);
if (!foregroundTaskAppName.equals("Your App name")) {
return true;
}
} catch (Exception e) {
Log.e("isAppSentToBackground", "" + e);
}
return false;
}
Answer updated again
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 am = (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;
}
Answer Updated
Check this link first Checking if an Android application is running in the background
http://developer.android.com/guide/topics/fundamentals.html#lcycles is a description of the Life Cycle of an android application.
The method onPause() gets called when the activity goes into the background. So you can deactivate the update notifications in this method.
public static boolean isApplicationSentToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
add permisions in the menifest as well
<uses-permission android:name="android.permission.GET_TASKS" />
Unfortunately, getRunningTasks() has been deprecated since Android API 21 (Android Lollipop):
This method was deprecated in API level 21. As of LOLLIPOP, this
method is no longer available to third party applications: the
introduction of document-centric recents means it can leak person
information to the caller. For backwards compatibility, it will still
return a small subset of its data: at least the caller's own tasks,
and possibly some other tasks such as home that are known to not be
sensitive.
Try this method to check your app is in background or not:
public boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
return isInBackground;
}
check this link & especially check the Answer Which shows this part of code
public class MyApplication extends Application {
public static boolean isActivityVisible() {
return activityVisible;
}
public static void activityResumed() {
activityVisible = true;
}
public static void activityPaused() {
activityVisible = false;
}
private static boolean activityVisible;
}
Hope this help you.

Service getting called multiple times?

I am developing an application where I am running some background operations and displaying notification to the user when certain condition meets .When the user clicks on the notification it should take him to the main activity , with out restarting the service .
My problem is when I am clicking the notification it takes me to the main activity and starting my service again.I dont want to restart my service again .How can I achieve this .
I have used the following code to check weather a service running or not lease look at it
public static boolean isServiceRunning(String serviceName,Context context){
boolean serviceRunning = false;
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50);
Iterator<ActivityManager.RunningServiceInfo> i = l.iterator();
while (i.hasNext()) {
ActivityManager.RunningServiceInfo runningServiceInfo = (ActivityManager.RunningServiceInfo) i
.next();
if(runningServiceInfo.service.getClassName().equals(serviceName)){
serviceRunning = true;
}
}
return serviceRunning;
}
I have used this piece of code and it's working fine :)
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
{
if (MyService.class.getName().equals(service.service.getClassName()))
{
found = true;
}
}
you could check getIntent() on the activity and put some extra on the intent bundle of the notification that you check to start or not the service.
Note:
if a service is already running, it is not restarted.
Finally I have rectified my code.as follows
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.sevicedemosolution.Myservice".equals(service.service.getClassName().toString())) {
return true;
}
}
return false;
}

How do I know if the home screen is in focus?

I have an service running on Android and I need to know if is any application on focus or if the "desktop" (home screen) is in focus. I don't know if this is the proper word to refer to the home screen of the phone. How can I know if this is in focus or some other application?
Inside the service I have this code to get the running tasks:
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;
How can I know from componentInfo if this is desktop or no? On emulator the componentInfo.getPackageName() returns com.android.launcher but in a Galaxy S1 (I tested only in this phone) returns something else.
There is any other way to do this?
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
ComponentName currentTask = taskInfo.get(0).topActivity;
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_HOME);
mPackageManager = getPackageManager();
List<ResolveInfo> appList = mPackageManager.queryIntentActivities(mainIntent, 0);
for(int i = 0; i < appList.size(); i++) {
ResolveInfo apl = appList.get(i);
if(currentTask.getPackageName().equals(apl.activityInfo.packageName)) {
Log.e(TAG, "ONHOMESCREEN");
}
}
And in AndroidManifest.xml you need permission
<uses-permission android:name="android.permission.GET_TASKS"/>
Try this function,
public boolean isUserIsOnHomeScreen()
{
ActivityManager manager =
(ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (RunningAppProcessInfo process : processes)
{
if(process.pkgList[0].equalsIgnoreCase("com.android.launcher"))
{
return true;
}
else
{
return false;
}
}
return false;
}
If you want to get control when "the home screen is in focus", implement a home screen.

Categories

Resources