When using the phone, when there is a missed call or a new short message, the phone will give a response to the message, these messages will generally appear on the phone screen status bar.
Android also provides classes for handling this information, which are notification and Notificationmanager.
, notification represents a global effect notification, while Notificationmanager is a system service for sending notification notifications.
Sending and displaying notifications using the notification and Notificationmanager classes is also relatively straightforward and can be broadly divided into the following four steps
(1) Call the Getsystemservice () method to get the system's Notificationmanager service
(2) Create a notification object and set various properties for it
(3) Setting event information for notification objects
(4) Sending notification notifications through the Notify () method of the Notificationmanager class
The following shows the notification on the status bar using an instance description and notification
International
Execution Result:
Layout file does not send a linear vertical layout two button
Mainactivity.class
Package Com.example.notification;import Android.os.bundle;import Android.app.activity;import Android.app.notification;import Android.app.notification.builder;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.content.context;import Android.content.intent;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity Implements Onclicklistener{private Notificationmanager manager;private button button1;private button button2;private int notification_id; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Setcontentview (R.layout.activity_main); manager= (Notificationmanager) Getsystemservice (Context.NOTIFICATION_ SERVICE), button1= (Button) Findviewbyid (R.id.button1), button2= (Button) Findviewbyid (R.id.button2); Button1.setonclicklistener (this); Button2.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {case r.id.button1:{shownotification (); Case R.id.button2:{manager.cancel (notification_id); private void Shownotification () {//TODO auto-generated method Stubnotification.builder builder=new Builder (this); Builder.setsmallicon (R.drawable.ic_launcher)///Set icon Builder.setticker ("Notifications come up");//Phone status bar prompt Builder.setcontenttitle ("I am the notification title");//Set the title Builder.setcontenttext ("I am the notification content");//Set the notification content Builder.setwhen (System.currenttimemillis ());// Set notification time Intent Intent=new Intent (this,mainactivity.class); Pendingintent pendingintent=pendingintent.getactivity (this, 0, intent, 0); Builder.setcontentintent (PendingIntent) ///Click on the Intent Builder.setdefaults (notification.default_lights);//Set LED Builder.setdefaults (Notification.default_ sound);//Set the audible builder.setdefaults (notification.default_vibrate);//Set the vibration Notification notification=builder.build ( );//4.1 above. below to use GetNotification () manager.notify (notification_id, Notification);}}
The indicator lights and vibrations are set in the code above, as the program has to access the system's LEDs and vibrators so you want to declare the use rights in Androidmanifest.xml
<uses-permission android:name= "Android.permission.VIBRATE"/> <uses-permission android:name= " Android.permission.FLASHLIGHT "/>
I hope to be of some help to you.
Slag residue learning ....
Android:notification implementing notifications for the status bar