我在后台服务GCM,我怎么知道,如果应用程序是开放与否? [英] I have a service gcm in the background, how do I know if the app is open or not?

查看:276
本文介绍了我在后台服务GCM,我怎么知道,如果应用程序是开放与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有延伸GCMBaseIntentService,一个类时,我得到GCM功能的消息:

@覆盖

 保护无效的onMessage(上下文的背景下,意图意图)
 {
     字符串消息= intent.getExtras()的GetString(外星人)。
     generateNotification(背景下,消息);
 }

我想,当有新邮件到达时,如果应用程序是开放的,用户使用,或者是一个简单的后台服务应用程序识别,因此,我看到一个通知。
我需要知道,如果它是在背景或前景,有什么建​​议?


解决方案

  

时如果应用程序是开放的,用户使用,或者是一个简单的后台服务,因此,我看到一个通知新邮件到达的应用程序识别


要做到这一点,您可以使用过程中的事件总线。有服务事件发布到总线上。有UI订阅总线这些事件时,UI在前台(如,注册在 onResume(),注销在的onPause() )。有UI过程中的事件时,UI得到他们。如果UI不响应该事件,该服务可以再提升一个通知

我有证明这三个流行事件总线实现Android的示例应用程序:

I have a class that extends GCMBaseIntentService, when I get a message from gcm function:

@Override

 protected void onMessage (Context context, Intent intent)
 {
     String message = intent.getExtras().GetString("alien");
     generateNotification (context, message);
 }

I wish that when a new message arrives the app recognize if the application is open and the user is using, or is a simple background service, and therefore I see a notification. I need to know if it is in the background or foreground, any suggestions ??

解决方案

when a new message arrives the app recognize if the application is open and the user is using, or is a simple background service, and therefore I see a notification

To do that, you can use an in-process event bus. Have the service post an event to the bus. Have the UI subscribe to the bus for those events when the UI is in the foreground (e.g., register in onResume(), unregister in onPause()). Have the UI process the events when the UI gets them. If the UI does not respond to the event, the service can then raise a Notification.

I have sample apps that demonstrate this for three popular event bus implementations for Android:

这篇关于我在后台服务GCM,我怎么知道,如果应用程序是开放与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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