Design template for an alternative action if the application is in the foreground?

I am creating an action that uses Google Cloud Messaging to receive push notifications.

This includes a couple of important components, namely:

  • A WakefulBroadcastReceiverto receive a message from GCM.
  • An IntentServicefor notification of a GCM event.

As I am sure this applies to many applications, I would like to use a different approach when the application has activity in the foreground, and also when the application does not have the application in the foreground.

I want to publish a notification in the notification box if the application is inactive, and just forward the event to the foreground activity if the application is active.

Is there a design template for this? How can I tell if an application is in the foreground and background?

+3
source share
2 answers

You can follow the approach described in this blog post where you create BroadcastReceiverin your own Activitywith a positive priority (i.e. higher than the default value 0) that intercepts a specific action and then calls abortBroadcast- this handles the case of opening the application. Then you also register BroadcastReceiverin your manifest, which processes the same action, but the default priority is the reserve if the application is not open and creates a system notification.

sendOrderedBroadcast() . , . .

+3

, .

GCM ( IntentService, , , Service).

, , (, ), .

. onPause() onResume(). onResume() . , " , , ... , -", ( mActivity). onPause() , , mActivity = null, .

, push-, mActivity, , . , ...

+2

All Articles