Android: Use of the notification bar

Source: Internet
Author: User

Android: Use of the notification bar

I haven't used the Android notification function for a long time. Today I moved out the code two years ago and found that many methods have been discarded, and the various strikethroughs in the Code are quite uncomfortable. Then, Open Google, view official documents, and learn the latest method for sending notification bar messages.

 

1. First, obtain the system Notification Service:

 

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2. Send the simplest notification

 

 

Public void simpleNotice (View view) {// This Builder is in android. support. v4.app. icationicationcompat. Builder, the same below. Builder mBuilder = new Builder (this); // text displayed on the notification bar when the system receives the notification. MBuilder. setTicker (Tianjin, Qing, 2 ~ 15 degrees, breeze); // a small mBuilder icon displayed on the notification bar. setSmallIcon (R. drawable. consult_answer); // notification title mBuilder. setContentTitle (weather forecast); // notification content mBuilder. setContentText (Tianjin, Qing, 2 ~ 15 degrees, breeze); // set a large icon, that is, the picture on the left side of the notification bar (if only a small icon is set, a small icon is displayed here) mBuilder. setLargeIcon (BitmapFactory. decodeResource (getResources (), R. drawable. share_sina); // The number mBuilder displayed on the left of the small icon. setNumber (6); // It is set to the non-purge mode mBuilder. setOngoing (true); // display the notification. The id must be unique. Otherwise, the new notification will overwrite the old one (with this feature, you can update the notification) nm. notify (1, mBuilder. build ());}
3. delete a notification. The parameter is the notification id.
nm.cancel(1);
4. Send a notification, click the notification, and jump to an Activity. After the Activity is returned, enter a page in the Program (usually the homepage)

 

 

// Click notification to enter an Activity. When you click return, the specified page is displayed. Public void resultActivityBackApp (View view) {Builder mBuilder = new Builder (this); mBuilder. setTicker (Notification Title 2); mBuilder. setSmallIcon (R. drawable. ic_launcher); mBuilder. setContentTitle (Notification Title 2); mBuilder. setContentText (Click notification to enter an Activity, and enter the specified page when you click return .); // Set to disappear after one click (this method is invalid if no click event is found .) MBuilder. setAutoCancel (true); // Intent resultIntent = new Intent (this, ResultActivityBackApp. class); // use TaskStackBuilder to set the return relationship TaskStackBuilder stackBuilder = TaskStackBuilder for the "Notification page. create (this); // set the return page for the page opened after clicking the notification. (Specified in manifest) stackBuilder. addParentStack (ResultActivityBackApp. class); stackBuilder. addNextIntent (resultIntent); PendingIntent pIntent = stackBuilder. getPendingIntent (0, PendingIntent. FLAG_UPDATE_CURRENT); mBuilder. setContentIntent (pIntent); // mId allows you to update the notification later on. nm. notify (2, mBuilder. build ());}
In addition, you must specify the parent Activity for the Activity opened after clicking the notification in manifest.

 

 

            
         
 
(The activity property parentActivityName is the property in API 16, and the code in meta-data is less than compatible with API 16. Therefore, for most programs, both of them have to be written .)

 

5. Similar to the above 4, the home page is returned only when the Activity is returned.

 

// Click the notification to enter an Activity. When you click return, return to the desktop public void resultActivityBackHome (View view) {Builder mBuilder = new Builder (this); mBuilder. setTicker (Notification Title 3); mBuilder. setSmallIcon (R. drawable. ic_launcher); mBuilder. setContentTitle (Notification Title 3); mBuilder. setContentText (Click notification to go to an Activity and return to the desktop when you click return); // set to disappear after one click (if no click event is displayed, this method is invalid .) MBuilder. setAutoCancel (true); Intent policyintent = new Intent (this, ResultActivityBackHome. class); policyintent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); PendingIntent pIntent = PendingIntent. getActivity (this, 0, policyintent, PendingIntent. FLAG_UPDATE_CURRENT); mBuilder. setContentIntent (pIntent); nm. notify (3, mBuilder. build ());}
6. Notifications with progress bars

 

 

Public void progressNotice (View view) {final Builder mBuilder = new Builder (this); mBuilder. setTicker (Notification Title 4); mBuilder. setContentTitle (Picture Download ). setContentText (Download in progress ). setSmallIcon (R. drawable. ic_launcher); // Start a lengthy operation in a background thread new Thread (new Runnable () {@ Override public void run () {int progress; for (progress = 0; progress <= 100; progress ++) {// Sets the progress indicator to a max value, the current completion percentage, // and determinate state mBuilder. setProgress (100, progress, false); // progress bar with unclear progress // mBuilder. setProgress (0, 0, true); nm. notify (4, mBuilder. build (); // simulate the latency try {Thread. sleep (200);} catch (InterruptedException e) {e. printStackTrace () ;}// When the loop is finished, updates the notification mBuilder. setContentText (Download complete); // Removes the progress bar mBuilder. setProgress (0, 0, false); nm. notify (4, mBuilder. build ());}}). start ();}
7. Notifications of extended layout. Press and hold the notification bar to see more details.

 

 

Public void expandLayoutNotice (View view) {Builder mBuilder = new Builder (this); mBuilder. setTicker (Notification Title 5); mBuilder. setSmallIcon (R. drawable. ic_launcher); mBuilder. setContentTitle (Notification Title 5); mBuilder. setContentText (Press and hold the notification drop-down to display the expanded layout); icationicationcompat. inboxStyle inboxStyle = new NotificationCompat. inboxStyle (); String [] events = new String [] {Beijing, Tianjin, Shanghai, Guangzhou}; // set the expanded layout title inboxStyle. setBigContentTitle (Event tracker details :); for (String s: events) {inboxStyle. addLine (s);} mBuilder. setStyle (inboxStyle); nm. notify (5, mBuilder. build ());}

8. custom Layout notification bar. (This is not recommended according to Google's official documentation, because there are too many factors to consider when using this method to adapt to different screens. In addition, the notification bar should display the most concise information, which is sufficient for the default layout of most programs .)

 

 

// Notification of custom Layout public void customLayoutNotice (View view) {Builder mBuilder = new Builder (this); mBuilder. setTicker (Notification Title 6); mBuilder. setTicker (Notification Title 6); mBuilder. setSmallIcon (R. drawable. ic_launcher); RemoteViews remoteViews = new RemoteViews (getPackageName (), R. layout. custom_layout_notice); mBuilder. setContent (remoteViews); // set the text RemoteViews for the buttons on remoteViews. setCharSequence (R. id. custom_layout_button1, se TText, Button1); remoteViews. setCharSequence (R. id. custom_layout_button2, setText, Button2); // set the event Intent intent1 = new Intent (this, CustomLayoutResultActivity for the buttons on RemoteViews. class); intent1.putExtra (content, From button1 click !); PendingIntent pIntentButton1 = PendingIntent. getActivity (this, 0, intent1, PendingIntent. FLAG_UPDATE_CURRENT); remoteViews. setOnClickPendingIntent (R. id. custom_layout_button1, pIntentButton1); Intent intent2 = new Intent (this, CustomLayoutResultActivity. class); intent2.putExtra (content, From button2 click !); PendingIntent pIntentButton2 = PendingIntent. getActivity (this, 1, intent2, PendingIntent. FLAG_UPDATE_CURRENT); remoteViews. setOnClickPendingIntent (R. id. custom_layout_button2, pIntentButton2); nm. notify (6, mBuilder. build ());}


 

Related Article

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.