简体   繁体   English

android:如何检查应用程序是否在后台运行

[英]android:how to check if application is running in background

I am newbie to android. 我是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. 服务器每隔一分钟就会继续向客户端发送更新通知,在客户端,我的应用程序会收到这些更新并使用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. 我没有得到如何检查该应用程序是否在后台运行。

Update, see this first: 更新,请先看看:

Checking if an Android application is running in the background 检查Android应用程序是否在后台运行


To check if your application is sent to background, you can call this code on onPause() on every activity in your application: 要检查您的应用程序是否已发送到后台,您可以在应用程序中的每个活动上的onPause()上调用此代码:

 /**
   * 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 为此,您应该在AndroidManifest.xml包含它

<uses-permission android:name="android.permission.GET_TASKS" />

http://developer.android.com/guide/topics/fundamentals.html#lcycles is a description of the Life Cycle of an android application. http://developer.android.com/guide/topics/fundamentals.html#lcycles是对Android应用程序生命周期的描述。

The method onPause() gets called when the activity goes into the background. 当活动进入后台时,会调用onPause()方法。 So you can deactivate the update notifications in this method. 因此,您可以在此方法中停用更新通知。

Only for API level 14 and above 仅适用于API级别14及更高级别

You can use ComponentCallbacks2 to an activity , service , etc. 您可以将ComponentCallbacks2用于activityservice等。

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
     }
   }
}

您可以在ActivityManager中使用getRunningAppProcesses()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM