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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3183932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:03:52  来源:igfitidea点击:

android:how to check if application is running in background

android

提问by Rise

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.

我是安卓的新手。我有基于客户端服务器的应用程序。服务器每分钟后不断向客户端发送更新通知,在客户端,我的应用程序接收这些更新并使用 Toast 显示它。但现在我的问题是,每当我的客户端应用程序进入后台服务器时,都会不断发送更新通知,而我的客户端将其显示为应用程序在前台。我不知道如何检查应用程序是否在后台运行。

采纳答案by nuriaion

http://developer.android.com/guide/topics/fundamentals.html#lcyclesis 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. So you can deactivate the update notifications in this method.

当活动进入后台时调用 onPause() 方法。因此,您可以在此方法中停用更新通知。

回答by peceps

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

回答by Rohit Arya

Only for API level 14 and above

仅适用于 API 级别 14 及以上

You can use ComponentCallbacks2to an activity, service, etc.

您可以使用ComponentCallbacks2activityservice等等。

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

回答by metdos

You can use getRunningAppProcesses()in ActivityManager .

您可以在 ActivityManager 中使用getRunningAppProcesses()