Android Status bar Alert
The Alertdialog can also be used in Android, but we need to use it cautiously, because when Alertdialog is used, the user's ongoing operations will be interrupted because the current focus is alertdialog. We can imagine that when the user play the game is good, this time a text message. If this is a message with a alertdialog reminder, the user must first handle the reminder to continue the game. The user may be angry and die alive. The use of notification will not bring these problems, users can completely play the game to see this message. Therefore, in the development should be based on the actual requirements, select the appropriate control.
Steps:
One, add a layout object
Copy Code code as follows:
<button
Android:id= "@+id/showbutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Shownotification"/>
<button
Android:id= "@+id/cancelbutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Cancelnotification"/>
Modify the activity of Mianactivity inheritance and implement Interface Onclicklistener
Copy Code code as follows:
public class Mainactivity extends activity implements Onclicklistener {
Private context Mcontext = this;
Private Button showbtn, calclebtn;
Private Notification Noti;
Private Notificationmanager Notimanger;
private static int notification_id = 0x0001;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Setupviews ();
}
private void Setupviews () {
showbtn = (Button) Findviewbyid (R.id.showbutton);
&NBSP;&NBSP;CALCLEBTN = (Button) Findviewbyid (R.id.cancelbutton);
noti = new Notification (R.drawable.ic_launcher, "This is a Notification", System.currenttimemillis ());
noti.defaults = notification.default_sound;//Use the default prompt sound
noti.defaults |= notification.default_vibrate;//Add Vibration
notimanger = (notificationmanager) this.getsystemservice ( Mcontext.notification_service);//Get Nofificationmanger Object
showbtn.setonclicklistener (this);// Let the activity implement Interface Onclicklistener can simply add a button to the two lines of code click Response Event
calclebtn.setonclicklistener (this);
.}
button click event Response
@Override
public void OnClick (View v) {
if (v = = showbtn) {
Intent Intent = new Intent (This.getapplicationcontext (), This.getclass ());
Set Intent.flag_activity_new_task
Intent.setflags (Intent.flag_activity_new_task);
Pendingintent contentintent = pendingintent.getactivity (this, 0, intent, 0);
Noti.setlatesteventinfo (contexts, Contenttitle, ContentText, contentintent) settings (context, title, content, Pendinginteng)
Noti.setlatesteventinfo (This, "10086", "you waive all charges from now on", contentintent);
Send notification (message ID, notification object)
Notimanger.notify (notification_id, Noti);
else if (v = = calclebtn) {
Cancellation notification (ID)
Notimanger.cancel (notification_id);
}
}
}