A small demo. Click the Send notification button to send a notification to the device's notification bar. Click Clear notifications to clear message notifications on the notification bar.
Package Zhangphil.notification;import Android.os.bundle;import Android.view.view;import Android.widget.Button; Import Android.app.activity;import Android.app.notification;import Android.app.notificationmanager;import Android.support.v4.app.notificationcompat;public class Mainactivity extends Activity {private final int notification_ ID = @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); button send = (Button) Findviewbyid (r.id.send), Send.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {sendnotification ();}}); Button clear = (Button) Findviewbyid (r.id.clear); Clear.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {clearnotification ();}}); private void SendNotification () {Notificationmanager Notificationmanager = (notificationmanager) getsystemservice ( Notification_service); Notificationcompat.builder Mbuilder = new notificationcompat.bUilder (this); Mbuilder.setsmallicon (R.drawable.ic_launcher); Mbuilder.setcontenttitle ("title of the Notice"); Mbuilder.setcontenttext ("Content of the notice"); Notification Notification = Mbuilder.build ();//The default setting is when sending notifications to the notification bar: Alert sound + phone shake notification.defaults = notification.default_sound| notification.default_vibrate;//notification Time Notification.when = System.currenttimemillis ();// The notification bar sent to the phone notificationmanager.notify (notification_id, NOTIFICATION);} private void Clearnotification () {Notificationmanager Notificationmanager = (notificationmanager) getsystemservice ( Notification_service); Notificationmanager.cancel (notification_id);}}
The Activity_main.xml needed
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:orientation=" vertical "> <button Android:id= "@+id/send" android:layout_width= "wrap_content" android:layout_height= "Wrap_content " android:text= "Send notification"/> <button android:id= "@+id/clear" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "purge notification"/></linearlayout>
Android Notification notification