Android-System and custom notification

Source: Internet
Author: User

Notification is a way for your application to alert users without using activity, and notification is an invisible program component that warns users of the best ways to take notice of events.

As part of the UI, notification is the best fit for mobile devices. Users may always have their phone with them. In general, users will open several programs in the background, but will not notice them. In such a situation, it is important to be able to notify the user when an event that needs attention occurs.

Notification is managed by Notificationmanger, and currently includes the following competencies:

    • Creates a status bar icon.
    • Displays additional information in the Extended Status bar window (and launches a intent).
    • Flashing lights or LEDs.
    • The phone vibrates.
    • Audible audible warning (ringtone, saved sound file).

Invoking the notification of the system API

 Public voidclick1 (view view) {//1. Administrators who get system notificationsNotificationmanager nm =(Notificationmanager) Getsystemservice (Notification_service); //2. Initializing an Notification objectNotification Notification =NewNotification (r.drawable.notification, "I am scrolling text", System.currenttimemillis ()); //3. Set the specific parameters of the notificationNotification.flags =Notification.flag_auto_cancel; //Notification.sound = Uri.parse (uristring);Intent Intent =NewIntent ();         Intent.setaction (Intent.action_view); Intent.setdata (Uri.parse ("Http://www.baidu.com")); Pendingintent contentintent= Pendingintent.getactivity ( This, 0, intent, 0); Notification.setlatesteventinfo ( This, "I am the big headline", "I am the content below the title", contentintent); //4. Direct the message to the Notification ManagerNm.notify (0, notification); }

Custom notification

 Public classMainactivityextendsActivity {protected intmprogress; PrivateNotification Notification; PrivateRemoteviews RV; PrivateNotificationmanager nm; Privatependingintent contentintent; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClicklala (view view) {//1. Administrators who get system notificationsNM =(Notificationmanager) Getsystemservice (Notification_service); //2. Initializing an Notification object        Longwhen =System.currenttimemillis (); String Tickertext= "IIIIPPPP"; Notification=NewNotification (R.drawable.ic_launcher,tickertext,when); //3. Set the specific parameters of the notification//cannot be manually cleaned outNotification.flags =Notification.flag_auto_cancel; //Notification.contentview = Custom notification ViewRV =Newremoteviews (Getpackagename (), r.layout.notify); Rv.settextviewtext (R.ID.TV_RV,"I'm a custom notification."); Rv.setprogressbar (R.ID.PB_RV,100, 0,false); Notification.contentview=RV; Intent Intent=NewIntent ();        Intent.setaction (Intent.action_view); Intent.setdata (Uri.parse ("Http://www.baidu.com")); Contentintent= Pendingintent.getactivity ( This, 0, intent, 0); Notification.contentintent=contentintent; //4. Direct the message to the Notification ManagerNm.notify (0, notification); //initiates a multi-threaded update notification reminder.         NewThread (NewRunnable () { Public voidrun () { for(inti = 0; I < 20; i++) {mprogress= (i + 1) * 5; Try {                          if(I < 19) {Thread.Sleep (1000); } Else{Thread.CurrentThread (). interrupt (); }                      } Catch(interruptedexception e) {e.printstacktrace (); } Message Message=NewMessage ();                  Mhandler.sendmessage (message);    }}). Start (); }    PrivateHandler Mhandler =NewHandler () { Public voidhandlemessage (Message message) {//sets the progress value of the progress bar in the RemoteView component. Rv.setprogressbar (R.ID.PB_RV, Mprogress,false); Rv.settextviewtext (R.ID.TV_RV,"Download:" + mprogress + "%"); //set the layout for notification. Notification.contentview =RV; /to set intent for notification, click Notification to issue this intent. Notification.contentintent=contentintent; //Send notification reminders. Nm.notify (0, notification); Super. Handlemessage (message);  }      }; }

Layout:

<?XML version= "1.0" encoding= "Utf-8"?>  <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">   <TextViewAndroid:id= "@+id/tv_rv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "haha"   />   <ProgressBarstyle= "@android: Style/widget.progressbar.horizontal"Android:id= "@+id/pb_rv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"   />   </LinearLayout>

I'm the dividing line of the king of the Land Tiger.

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Notification Getting started. zip

Reprint Please specify source: Http://www.cnblogs.com/yydcdut

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.