检查是否一个Android应用程序在后台运行应用程序、后台、Android

2023-09-11 23:52:14 作者:我用整个曾经去爱你

到后台,我的意思是没有的应用程序的活动是当前可见的用户?

解决方案

有几种方法来检测后台应用程序是否正在运行,但其中只有一个是完全可靠的:

正确的解决方案(学分转到丹,的a一些变化此解决方案,如果你想从服务检查活动的可见性)。   示例的 实现自定义的应用程序类(注意 isActivityVisible()静态方法):

 公共类MyApplication的扩展应用{

  公共静态布尔isActivityVisible(){
    返回activityVisible;
  }

  公共静态无效activityResumed(){
    activityVisible = TRUE;
  }

  公共静态无效activityPaused(){
    activityVisible = FALSE;
  }

  私有静态布尔activityVisible;
}
 

填写您的应用程序类的的Andr​​oidManifest.xml

 <应用
    机器人:名称=your.app.package.MyApplication
    机器人:图标=@可绘制/图标
    机器人:标签=@字符串/ APP_NAME>
 
怎么创建第一个Android工程,并运行应用程序

添加的onPause onResume 活动的该项目(您可以创建一个共同的祖先为您的活动,如果你想,但如果你的活动是从 MapActivity / ListActivity 等,你仍然需要手工编写以下):

  @覆盖
保护无效onResume(){
  super.onResume();
  MyApplication.activityResumed();
}

@覆盖
保护无效的onPause(){
  super.onPause();
  MyApplication.activityPaused();
}
 

  更新的 ActivityLifecycleCallbacks添加在API级别14(Android 4.0的)。你可以用它们来跟踪你的应用程序的活动是否是当前可见的用户。检查下面玉米秆的回答了解详情。

错了 我曾经提出了以下解决方案:

       

您可以检测当前前景与ActivityManager.getRunningAppProcesses()返回 RunningAppProcessInfo 记录的列表。要确定您的应用程序在前台检查 RunningAppProcessInfo.importance 字段平等 RunningAppProcessInfo.IMPORTANCE_FOREGROUND ,而 RunningAppProcessInfo.processName 等于你的应用程序包的名称。

         

另外,如果你调用 ActivityManager.getRunningAppProcesses()从您的应用程序UI线程将返回的重要性 IMPORTANCE_FOREGROUND 您任务无论它实际上是在前台与否。 (通过的AsyncTask )后调用它在后台线程,它会返回正确的结果。

  

虽然这种解决方案可能工作(和它确实工作的大部分时间)我强烈建议使用它来避免。这里的原因。 作为戴安娜Hackborn写道:

       

这些API是不存在的应用程序,以立足于自己的用户界面流,而是做事情像显示用户运行的应用程序或任务管理器,或此类。

         

是有保存在内存中为这些事情的清单。然而,关在另一个进程中,从你单独运行的线程,而不是你可以在时间,使被返回的时间正确的决定,或(b)有一个一致的画面(A)上看到数管理。加什么下一个活动去进行时,总是在点开关的情况发生,这是不是直到确切点(其中的活动状态进行了简要锁定做开关)的决定,我们实际上肯定知道是什么,接下来的事情会。

         

和此处实施和全局行为是不能保证在未来保持相同。

  

我希望我读这之前,我贴在SO答案,但希望这不是为时已晚承认我的错误。

另一种错误的解决方法 Droid-Fu在 ActivityManager.getRunningTasks isApplicationBroughtToBackground 方法的一个答案用途库提。见戴安娜的评论上面,不要使用该方法无论是。

By background, I mean none of the application's activities are currently visible to the user?

解决方案

There are few ways to detect whether your application is running in the background, but only one of them is completely reliable:

The right solution (credits go to Dan, CommonsWare and NeTeInStEiN) Track visibility of your application by yourself using Activity.onPause, Activity.onResume methods. Store "visibility" status in some other class. Good choices are your own implementation of the Application or a Service (there are also a few variations of this solution if you'd like to check activity visibility from the service). Example Implement custom Application class (note the isActivityVisible() static method):

public class MyApplication extends Application {

  public static boolean isActivityVisible() {
    return activityVisible;
  }  

  public static void activityResumed() {
    activityVisible = true;
  }

  public static void activityPaused() {
    activityVisible = false;
  }

  private static boolean activityVisible;
}

Register your application class in AndroidManifest.xml:

<application
    android:name="your.app.package.MyApplication"
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

Add onPause and onResume to every Activity in the project (you may create a common ancestor for your Activities if you'd like to, but if your activity is already extended from MapActivity/ListActivity etc. you still need to write the following by hand):

@Override
protected void onResume() {
  super.onResume();
  MyApplication.activityResumed();
}

@Override
protected void onPause() {
  super.onPause();
  MyApplication.activityPaused();
}

Update ActivityLifecycleCallbacks were added in API level 14 (Android 4.0). You can use them to track whether an activity of your application is currently visible to the user. Check Cornstalks' answer below for the details.

The wrong one I used to suggest the following solution:

You can detect currently foreground/background application with ActivityManager.getRunningAppProcesses() which returns a list of RunningAppProcessInfo records. To determine if your application is on the foreground check RunningAppProcessInfo.importance field for equality to RunningAppProcessInfo.IMPORTANCE_FOREGROUND while RunningAppProcessInfo.processName is equal to your application package name.

Also if you call ActivityManager.getRunningAppProcesses() from your application UI thread it will return importance IMPORTANCE_FOREGROUND for your task no matter whether it is actually in the foreground or not. Call it in the background thread (for example via AsyncTask) and it will return correct results.

While this solution may work (and it indeed works most of the time) I strongly recommend to refrain from using it. And here's why. As Dianne Hackborn wrote:

These APIs are not there for applications to base their UI flow on, but to do things like show the user the running apps, or a task manager, or such.

Yes there is a list kept in memory for these things. However, it is off in another process, managed by threads running separately from yours, and not something you can count on (a) seeing in time to make the correct decision or (b) have a consistent picture by the time you return. Plus the decision about what the "next" activity to go to is always done at the point where the switch is to happen, and it is not until that exact point (where the activity state is briefly locked down to do the switch) that we actually know for sure what the next thing will be.

And the implementation and global behavior here is not guaranteed to remain the same in the future.

I wish I had read this before I posted an answer on the SO, but hopefully it's not too late to admit my error.

Another wrong solution Droid-Fu library mentioned in one of the answers uses ActivityManager.getRunningTasks for its isApplicationBroughtToBackground method. See Dianne's comment above and don't use that method either.