Android in the notification and Notificationmanger of the detailed _android

Source: Internet
Author: User

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);
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.