Display Alert when push arrives

别来无恙 提交于 2019-12-05 14:40:56

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!