Details about Notification and NotificationManger in Android

Source: Internet
Author: User

Android status bar reminder

AlertDialog can also be used in the reminder function of Android, but we should be careful when using AlertDialog, because when using AlertDialog, the user's ongoing operations will be interrupted, because the current focus is obtained by AlertDialog. We can imagine that a text message was sent when a user played a game well. If AlertDialog is used as a reminder, you must handle this reminder before continuing the game. The user may be angry. However, using Notification will not cause these troubles. Users can play the game and then read this text message. Therefore, select appropriate controls based on actual needs during development.

Steps:

1. Add layout objects

Copy codeThe Code is 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"/>

2. Modify the MianActivity inheritance part and implement the OnClickListener interface.
Copy codeThe Code is as follows: public class MainActivity extends Activity implements OnClickListener {
Private Context mContext = this;
Private Button showbtn, calclebtn;
Private Notification noti;
Private icationicationmanager notiManger;
Private static int icationication_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 );
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 a vibration
NotiManger = (icationicationmanager) this. getSystemService (mContext. icationication_service); // obtain the NofificationManger object
Showbtn. setOnClickListener (this); // enables the Activity to implement the OnClickListener interface. You can simply click the Add button in the two lines of code and then click the RESPONSE event.
Calclebtn. setOnClickListener (this );
}

// Click the button to respond to the event
@ 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 (context, contentTitle, contentText, contentIntent) settings (context, title, content, PendingInteng)
Noti. setLatestEventInfo (this, "10086", "you will be exempted from all calls", contentIntent );
// Send a notification (Message ID, notification object)
NotiManger. Y (icationication_id, noti );
} Else if (v = calclebtn ){
// Cancel notification (id)
NotiManger. cancel (icationication_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.