10

I am newbie to android. I have client server based application. Server keeps on sending the update notifications to client after every single minute and at client side my app receive those updates and display it using Toast. But now my problem is whenever my client app goes into the background server keeps on sending the update notifications and my client display it as if the application is in foreground. I am not getting how to check that application is running in background.

Janusz
  • 187,060
  • 113
  • 301
  • 369
Rise
  • 820
  • 4
  • 18
  • 33

4 Answers4

37

Update, see this first:

Checking if an Android application is running in the background


To check if your application is sent to background, you can call this code on onPause() on every activity in your application:

 /**
   * Checks if the application is being sent in the background (i.e behind
   * another application's Activity).
   * 
   * @param context the context
   * @return <code>true</code> if another application will be above this one.
   */
  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;
  }

For this to work you should include this in your AndroidManifest.xml

<uses-permission android:name="android.permission.GET_TASKS" />
Community
  • 1
  • 1
peceps
  • 17,370
  • 11
  • 72
  • 79
  • 1
    Hi @peceps I want to inform you that with Android 4.4 (or N5) this method is not working properly. topActivity and context packages are equals... can you please update your answer? – StErMi Nov 11 '13 at 15:30
  • @StErMi http://stackoverflow.com/questions/3667022/android-is-application-running-in-background – Raheel Dec 21 '13 at 08:23
  • The method [`getRunningTasks (int maxNum)`, was deprecated in the API level 21(LOLLIPOP)](http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks(int)), for security reasons, the possibility of leaking personal information to third-party applications. (If you use this method to be aware that it will not be supported for future Android versions). – Fernando Leal Jan 12 '15 at 15:49
  • What does this line do? `!topActivity.getPackageName().equals(context.getPackageName())` – Ruchir Baronia Nov 11 '15 at 20:29
8

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.

nuriaion
  • 2,621
  • 2
  • 23
  • 18
3

Only for API level 14 and above

You can use ComponentCallbacks2 to an activity, service, etc.

Example:

public class MainActivity extends AppCompatActivity implements ComponentCallbacks2 {
   @Override
   public void onConfigurationChanged(final Configuration newConfig) {

   }

   @Override
   public void onLowMemory() {

   }

   @Override
   public void onTrimMemory(final int level) {
     if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
        // app is in background
     }
   }
}
Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
0

You can use getRunningAppProcesses() in ActivityManager .

metdos
  • 13,411
  • 17
  • 77
  • 120