设计模式的交替动作,如果应用程序在前台? [英] Design pattern for alternate action if app in foreground?

查看:132
本文介绍了设计模式的交替动作,如果应用程序在前台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建,它利用谷歌的云信息接收推送通知的动作。

I'm building an action which utilizes Google Cloud Messaging for receiving push notifications.

这涉及到几个显着的部分,即:

This involves a couple notable components, namely:

  • A WakefulBroadcastReceiver 从GCM接收消息。
  • IntentService 收到GCM事件时得到通知。
  • A WakefulBroadcastReceiver to receive the message from GCM.
  • An IntentService to be notified when a GCM event is received.

我敢肯定的是许多应用的情况下,我想采取不同的方法,当应用程序在前台与当应用程序做的没有的有一个活动在前台应用程序。

As I'm sure is the case for many applications, I'd like to take a different approach for when the application has an activity in the foreground vs. when the application does not have an application in the foreground.

我要发布一个通知,通知抽屉如果应用程序不活跃,并简单地将事件转发到前台活动如果应用程序处于活动状态。

I'd like to post a notification to the notification drawer if the application is not active, and simply forward the event onto the foreground activity if the application is active.

有一个设计模式呢?我应该如何去判断,如果应用程序是在前台与后台?

Is there a design pattern for this? How should I go about determining if the app is in the foreground vs. the background?

推荐答案

您可以遵循的做法作为的这篇博客创建其中的BroadcastReceiver 活动以积极的优先级(即,比默认的高 0 )的拦截一个特定的动作,然后调用 abortBroadcast - 该处理程序打开的情况下。这是回退,如果应用程序是不开放的,它会创建系统通知 - 然后你也在你的清单负责处理同样的动作,但在默认的优先级注册的BroadcastReceiver

You can follow an approach as outlined in this blog post where you create a BroadcastReceiver in your Activity with a positive priority (i.e., higher than the default of 0) which intercepts a specific action and then calls abortBroadcast - this handles the app open case. You then also register a BroadcastReceiver in your Manifest which handles the same action but at the default priority - this is the fallback if the app is not open and it creates the system notification.

您再使用 sendOrderedBroadcast()来你的广播发送到所有已注册的接收器中的优先顺序。如果活动是开放的,首先会处理它。否则,它会默认为另一个,并创建一个通知。

You then use sendOrderedBroadcast() to send your broadcast to all registered receivers in priority order. If the activity is open, the first will handle it. Otherwise, it will default to the other one and create a notification.

这篇关于设计模式的交替动作,如果应用程序在前台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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