OGeek|极客世界-中国程序员成长平台

标题: android - 检查Android应用程序是否在后台运行 [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-1 01:19
标题: android - 检查Android应用程序是否在后台运行

通过背景,我的意思是应用程序的 Activity 当前对用户都不可见?



Best Answer-推荐答案


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

  • 正确的解决方案 (学分转到 DanCommonsWareNeTeInStEiN )
    使用 Activity.onPause 自行跟踪应用程序的可见性, Activity.onResume方法。将“可见性”状态存储在其他类中。不错的选择是您自己实现 ApplicationService (如果您想检查服务的 Activity 可见性,也有此解决方案的 a few variations)。

    示例
    实现自定义 Application类(注意 isActivityVisible() 静态方法):
    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;
    }
    

    AndroidManifest.xml 中注册您的应用程序类:
    <application
        android:name="your.app.package.MyApplication"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
    

    添加 onPauseonResume给每个 Activity在项目中(如果您愿意,可以为您的 Activity 创建一个共同的祖先,但如果您的 Activity 已经从 MapActivity/ListActivity 等扩展,您仍然需要手动编写以下内容):
    @Override
    protected void onResume() {
      super.onResume();
      MyApplication.activityResumed();
    }
    
    @Override
    protected void onPause() {
      super.onPause();
      MyApplication.activityPaused();
    }
    

    更新
    ActivityLifecycleCallbacks在 API 级别 14 (Android 4.0) 中添加。您可以使用它们来跟踪您的应用程序的 Activity 当前是否对用户可见。查看Cornstalks' answer详情如下。
  • 打错了
    我曾经建议以下解决方案:

    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.



    虽然此解决方案可能有效(并且在大多数情况下确实有效),但我强烈建议不要使用它。这就是为什么。 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.



    我希望在我在 SO 上发布答案之前已经阅读过这篇文章,但希望现在承认我的错误还为时不晚。
  • 另一个错误的解决方案
    Droid-Fu答案之一中提到的库使用 ActivityManager.getRunningTasks为其isApplicationBroughtToBackground方法。请参阅上面 Dianne 的评论,也不要使用该方法。
  • 关于android - 检查Android应用程序是否在后台运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3667022/






    欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4