How Can I Display an alert when push notification arrives from C2DM or GCM. Right now I am displaying notifications on status bar. So How can I get a notification as an alert.
Popping a dialog in the middle of something a user is doing is a wildly user hostile thing to do. What if they're playing Angry Birds and you've just ruined their shot? Only the platform can and should get away with that.
Use the notification area, like Google intended, UNLESS you app currently has control (your activity is in running state). Then just use that activity's context to pop an AlertDialog()
. You can detect if the activity is running by overriding onResume()
and onPause()
- anything that happens in between is your activity's time.
An AlertDialog can be popped like this:
new AlertDialog.Builder(Ctxt) //Use an activity object here
.setMessage(R.string.MyMessageID) //Provide a message here... A string or a string ID will do
.setCancelable(true) //If you want them to be able to dismiss with a Back button
.setNegativeButton(R.string.IDS_NO, null) //No action on NO, right?
.setPositiveButton(R.string.IDS_YES, OnYesClickListener) //Plug your own listener...
.create()
.show();
For a simple message/Yes/No dialog, an AlertDialog
would suffice. For more complicated UI, derive a class from Dialog
and design your own layout.
For Alert you need to write code in
generateNotification(Context context, String message)
in this method.
Thanks
来源:https://stackoverflow.com/questions/12803557/display-alert-when-push-arrives