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