如何检测,如果你的Andr​​oid应用程序在前台(API 21) [英] How to detect if your android app is in the foreground (API 21)

查看:164
本文介绍了如何检测,如果你的Andr​​oid应用程序在前台(API 21)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,我的应用程序的行为取决于它是否在前台与否。此前API 21我用下面的:

when receiving a GCM message, the behaviour of my app depends on if it is in the foreground or not. Prior to API 21 I used the following:

Boolean onForeground = this.getPackageName().equalsIgnoreCase(
            ((ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE))
            .getRunningTasks(1).get(0).topActivity.getPackageName()
);

现在getRunningTasks()是pcated和API 21表现不同德$ P $,这将是在21 API的最佳方式来检测,如果我的应用程序是在前台?

Now getRunningTasks() is deprecated and behaving differently on API 21, what would be the best way on API 21 to detect if my app is in the foreground?

谷歌建议

public ActivityManager.RecentTaskInfo getTaskInfo ()

但我不知道怎样才能让我从那里需要的信息。

but I do not see how I can get the information I need from there.

感谢您提前。

编辑:的提议@CommonsWare和斯特拉顿@克里斯,我可以跟踪onResume /的onPause状态。一个应用程序崩溃的原因由的WebView(我的控制之外的内容)将离开该系统处于错误状态。在发送这种错误状态的GCM会引起其他崩溃混淆用户(他/她是不是做在我的应用程序的那一刻任何东西)。

As proposed by @CommonsWare and @Chris Stratton, I could keep track of onResume/onPause states. A app-crash cause by a WebView (content outside my control) would leave the system in the wrong state. Sending a GCM in such wrong state would cause an other crash confusing the user (he/she is not doing anything at that moment with my app).

因此​​,我正寻找另一种解决方案。
(我用的片段和只有1活性,使实施这个将是微不足道的)

I am therefore looking for another solution. (I am using Fragments and only have 1 activity, so implementing this would be trivial)

推荐答案

您可以使用的有序广播方式,你


  • 创建一个广播接收器在清单注册该做后台行为

  • 动态注册广播接收器在积极优先的活动(默认为 0 ),并确保在在onStart中注册() / onResume()的onStop中注销() / 的onPause() - 这样做前台的行为,应该叫<一个href=\"http://developer.android.com/reference/android/content/BroadcastReceiver.html#abortBroadcast()\"相对=nofollow> abortBroadcast()确保舱单注册广播接收器不叫

  • Create a BroadcastReceiver registered in your manifest which does the background behavior
  • Dynamically register a BroadcastReceiver in your activity with a positive priority (the default is 0), making sure to register it in onStart()/onResume() and unregistering it in onStop()/onPause() - this does the foreground behavior and should call abortBroadcast() to ensure the manifest registered BroadcastReceiver is not called

您GCM接收器就可以使用<一个href=\"http://developer.android.com/reference/android/content/Context.html#sendBroadcast(android.content.Intent)\"相对=nofollow> sendOrderedBroadcast()以(pferably $ P $自定义操作,无论你的其他广播接收器册174对)发送一个广播这将如果它存在和清单注册接收机,如果它不存在,由动态注册接收机接收。

Your GCM receiver can then use sendOrderedBroadcast() to send a broadcast (preferably with a custom action that both your other BroadcastReceivers register for) which will be received by the dynamically registered receiver if it exists and the manifest registered receiver if it does not exist.

这篇关于如何检测,如果你的Andr​​oid应用程序在前台(API 21)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆