Dowemo


Question:

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)


  • Best Answer:


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



    Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs