Android Development note--notification Notification bar

Source: Internet
Author: User

A small icon appears in the Android top status bar when a user has not received a call. Prompts the user to have no processing of the alerts, which can be viewed when dragging the status bar. Android gives us the Notificationmanager to manage this status bar. Can be done with ease.

Very basic things, directly read the comments on it, and paste it.

Look under:

1  PackageCom.example.notificationdemo;2 3 Importandroid.app.Activity;4 Importandroid.app.Notification;5 ImportAndroid.app.NotificationManager;6 Importandroid.app.PendingIntent;7 ImportAndroid.content.Context;8 Importandroid.content.Intent;9 ImportAndroid.os.Bundle;Ten ImportAndroid.view.View; One ImportAndroid.view.View.OnClickListener; A ImportAndroid.widget.Button; - ImportAndroid.widget.Toast; -  the  Public classMainactivityextendsActivityImplementsOnclicklistener { -  -     PrivateButton send; -     PrivateButton Cancel; +  -     Private intflag=888;//used to flag which notification is currently +     PrivateNotificationmanager Mnotificationmanager; A  at @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - Initview (); - Initlistener (); in         //Get notification system service -Mnotificationmanager =(Notificationmanager) Getsystemservice (context.notification_service); to     } +  -     Private voidInitlistener () { the          This. Send.setonclicklistener ( This); *          This. Cancel.setonclicklistener ( This); $     }Panax Notoginseng  -     Private voidInitview () { the          This. Send =(Button) Findviewbyid (r.id.send); +          This. Cancel =(Button) Findviewbyid (r.id.cancel); A     } the  + @Override -      Public voidOnClick (View v) { $         Switch(V.getid ()) { $          CaseR.id.send: -             //Send Notifications - sendnotification (); the              Break; -          CaseR.id.cancel:Wuyi             //Cancel Notification the mnotificationmanager.cancel (flag); -              Break; Wu         } -     } About  $     Private voidsendnotification () { -  -         //Build notification Display class -Notification.builder Builder =NewNotification.builder ( This); A         //set notification style content +Builder.setsmallicon (R.drawable.ic_launcher);//set a small icon at the top of the notification bar theBuilder.setcontenttitle ("I am the notification title bar");//set a specific title -Builder.setcontenttext ("I am the specific content of the notice");//Set specific content $Builder.setwhen (System.currenttimemillis ());//Set the time the         //set the light, vibrate, sound (can be set together Default_all, need corresponding permissions) the builder.setdefaults (notification.default_lights); the builder.setdefaults (notification.default_sound); the builder.setdefaults (notification.default_vibrate); -  inIntent Intent =NewIntent ( This, Mainactivity.class); thePendingintent pendingintent = pendingintent.getactivity ( This, 0, intent, 0); the  About         //set Click to jump Intent the builder.setcontentintent (pendingintent); the  the         //Set up Notifications + Notification Notification; -         if(Android.os.Build.VERSION.SDK_INT >= 16) { thenotification = Builder.build ();//System 4.1 or moreBayiToast.maketext ( This, "current system version 4.1 or higher", Toast.length_long). Show (); the}Else { thenotification = Builder.getnotification ();//system 4.1 or less -Toast.maketext ( This, "current system version 4.1 or less", Toast.length_long). Show (); -         } the  the         //Send Notifications the mnotificationmanager.notify (flag, notification); the     } -  the}

Android Development note--notification Notification bar

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.