I am creating an action that uses Google Cloud Messaging to receive push notifications.
This includes a couple of important components, namely:
WakefulBroadcastReceiver
IntentService
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?
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.
BroadcastReceiver
Activity
0
abortBroadcast
sendOrderedBroadcast() . , . .
sendOrderedBroadcast()
, .
GCM ( IntentService, , , Service).
Service
, , (, ), .
. onPause() onResume(). onResume() . , " , , ... , -", ( mActivity). onPause() , , mActivity = null, .
onPause()
onResume()
mActivity
mActivity = null
, push-, mActivity, , . , ...