1 PackageCom.lixu.tongzhi;
2 3 Importandroid.app.Activity;4 Importandroid.app.Notification;5 ImportAndroid.app.NotificationManager;6 ImportAndroid.os.Bundle;7 ImportAndroid.support.v4.app.NotificationCompat;8 ImportAndroid.view.View;9 ImportAndroid.view.View.OnClickListener;Ten ImportAndroid.widget.Button; One ImportAndroid.widget.Toast; A - Public classMainactivityextendsActivity { - the Private Static Final intID = 1987; - - @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + AButton BTN1 =(Button) Findviewbyid (r.id.fasong); atButton btn2 =(Button) Findviewbyid (R.id.qingchu); - -Btn1.setonclicklistener (NewOnclicklistener () { - - @Override - Public voidOnClick (View v) { in send (); - toToast.maketext (Getapplicationcontext (), "Send notification successfully!" ", 0). Show (); + - } the }); * $Btn2.setonclicklistener (NewOnclicklistener () {Panax Notoginseng - @Override the Public voidOnClick (View v) { + A Delete (); the +Toast.maketext (Getapplicationcontext (), "Clear notification succeeded!" ", 0). Show (); - $ } $ }); - - } the - Private voidSend () {Wuyi //Get Notification Manager theNotificationmanager Manager =(Notificationmanager) Getsystemservice (notification_service); - WuNotificationcompat.builder Mbuilder =NewNotificationcompat.builder ( This); - //set up a notification picture About Mbuilder.setsmallicon (R.DRAWABLE.SDFDF); $ //Set Title -Mbuilder.setcontenttitle ("Little Superman has come!" "); - //Set notification content -Mbuilder.setcontenttext ("I am a little Superman, the master has something to command.") "); A +Notification Notification =mbuilder.build (); the //set notification sounds or vibrations. -Notification.defaults = Notification.default_sound |notification.default_vibrate; $ //Notification Time theNotification.when =System.currenttimemillis (); the //send this notification via a specific ID the manager.notify (ID, notification); the - } in the Private voidDelete () { the //Get Notification Manager AboutNotificationmanager Manager =(Notificationmanager) Getsystemservice (notification_service); the Manager.cancel (ID); the the } + -}
Frequently used program notifications, displayed to the top bar of the main page.
XML file:
1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent" >5 6 <Button7 Android:id= "@+id/fasong"8 Android:layout_width= "Wrap_content"9 Android:layout_height= "Wrap_content"Ten Android:layout_alignleft= "@+id/qingchu" One Android:layout_alignparenttop= "true" A Android:layout_margintop= "56DP" - Android:text= "Send Notification" /> - the <Button - Android:id= "@+id/qingchu" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" + Android:layout_below= "@+id/fasong" - Android:layout_centerhorizontal= "true" + Android:layout_margintop= "88DP" A Android:text= "Clear Notification" /> at - </Relativelayout>
Run:
The notification notification on the top bar of the Android home page.