Android Notification notification

Source: Internet
Author: User

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

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.