当推送到达时显示警报 [英] Display Alert when push arrives
问题描述
当推送通知从C2DM或GCM到达时,如何显示警报。现在我在状态栏上显示通知。所以我怎样才能得到一个通知作为警报。
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.
使用通知区域,就像谷歌预期的那样,除非您的应用程序目前有控制权(您的活动处于运行状态)。然后使用该活动的上下文来弹出 AlertDialog()
。您可以通过覆盖 onResume()
和 onPause()
来检测活动是否正在运行 - 您的活动的时间。
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.
AlertDialog可以像这样弹出:
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();
对于简单的消息/是/否对话框, AlertDialog
就足够了。对于更复杂的UI,从 Dialog
派生一个类并设计你自己的布局。
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.
这篇关于当推送到达时显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!