android:how to check if application is running in background

后端 未结 4 1309
Happy的楠姐
Happy的楠姐 2020-12-01 18:53

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 ap

4条回答
  •  萌比男神i
    2020-12-01 19:34

    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 true if another application will be above this one.
       */
      public static boolean isApplicationSentToBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List 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

    
    

提交回复
热议问题