• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

android - 检查Android应用程序是否在后台运行

[复制链接]
菜鸟教程小白 发表于 2022-8-1 01:19:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

通过背景,我的意思是应用程序的 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/

    回复

    使用道具 举报

    懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    关注0

    粉丝2

    帖子830918

    发布主题
    阅读排行 更多
    广告位

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap