The notification notification on the top bar of the Android home page.

Source: Internet
Author: User

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.

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.