Android uses notification to implement notification management and custom notification bars (example four)

Source: Internet
Author: User

Example one: Implementing Notification bar Management

When notifications are made multiple times for the same type of event, as a developer, you should avoid using new notifications, and you should consider updating some of the values in the notification bar before you can alert the user. For example, the SMS system of our mobile phone, when new news comes, our notification bar is only change the number of text messages, instead of each text message to do a separate notification bar for tips.

Modify Notification

You can set a notification, of course, to update a notification, and we update it by using the ID that was used when calling notificationmanager.notify (ID, notification). To update your previously published notifications, you need to update or create a Notificationcompat.builder object, create a notification object from the previous notification, and then publish it using the ID you used earlier. If the previous notification is still visible, the system will update the contents of the notification directly, and a new notification will be created if the previous notification is not seen.

The following code shows an update to reflect the notification of the number of events that have occurred. It overlays the notification, which shows the summary:

(Note the presentation, the number of notifications accumulates and the notification bar disappears after clicking the notification)

Here we no longer demonstrate the Click button and jump page layout file, directly on the Java implementation code:

1 Importandroid.app.Notification;2 ImportAndroid.app.NotificationManager;3 Importandroid.app.PendingIntent;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 Importandroid.graphics.BitmapFactory;7 Importandroid.support.v7.app.AppCompatActivity;8 ImportAndroid.os.Bundle;9 ImportAndroid.support.v7.app.NotificationCompat;Ten ImportAndroid.view.View; One Importandroid.widget.RemoteViews; A  Public classMainactivityextendsappcompatactivity { -     Private Static Final intNo_1 = 0x1; -     intnum = 1;//number of initial notifications is 1 the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +     } -     //button click event (notification bar) +      Public voidShow1 (View v) { ANotificationcompat.builder Builder =NewNotificationcompat.builder ( This); at Builder.setsmallicon (r.mipmap.ic_launcher); -Builder.setcontenttitle ("New Message"); -Builder.setcontenttext ("You have a new message.")); -Builder.setnumber (num++); -         //after setting the click Notification Jump page, the notification disappears -Builder.setautocancel (true); inIntent Intent =NewIntent ( This, Main2activity.class); -pendingintent pi = pendingintent.getactivity ( This, 0, intent, pendingintent.flag_update_current); to builder.setcontentintent (pi); +Notification Notification =builder.build (); -Notificationmanager Notificationmanager =(Notificationmanager) Getsystemservice (context.notification_service); the notificationmanager.notify (no_1,notification); *     } $}

When we set Setautocancel () to False , the display effect is as follows (after clicking the notification bar does not disappear):

Example two: Customizing the notification bar

The notification framework allows you to customize the layout of notifications. Defines the appearance of the notification through the Remoteviews object. The custom notification layout is similar to regular notifications, but it is based on the Remoteviews object defined in the XML file.

The available height for custom notifications is dependent on the notification view. The normal view layout height is limited to 64DP, and the layout height of the expandable view is limited to 256DP.

To define your own notification layout, get an instance of the Remoteviews object from the curried XML file. Then, like calling the Setcontenttitle () method, we need to call SetContent (). In order to set more details, we use the Remoteviews object method to set more content.

1. Create a separate XML file to define the layout of the notification. You can use any name you want, but the suffix must be. Xml.

2. In the app, use the Remoteviews object method to set the text and icon for your notification, and put your remoteviews object into Notificationcompat.builder by calling SetContent (). Avoid using background images because your text may become less readable.

The Remoteviews object also contains some methods to add chronometer and ProgressBar to you. For more information on customizing the layout of the notification bar, refer to the remoteviews documentation.

Note: When you use a custom notification bar, pay special attention to how your custom notification bar works on devices with different orientations and resolutions. Of course, this recommendation is important for all view layouts. This is especially important for notification bars, because the control of the notification drawer is very limited. Do not make your own notice strips too complex to ensure that it is flexible.

Use a style resource for custom notification bar text (Usingstyle resources for custom notification text)

Custom notification bars always use style resources to define text. The background color of the notification becomes a big contrast with the device and the current version of Android. Using a style file can help you solve this problem very well. Starting with Android 2.3, the system defines the style of the text for the standard notification layout, and if you use the same style on Android2.3 and its higher version, you must make sure that your text is visible relative to the background.

Attention:

Notificationthe custom layout isremoteviews, and otherremoteviews, in a custom view layout file, only supportFramelayout,LinearLayout,Relativelayoutthree layout controls andAnalogClock,chronometer,Button,ImageButton,ImageView,ProgressBar,TextView,Viewflipper,ListView,GridView,StackViewand theAdapterviewflipperthese display controls, which do not support subclasses of these classes orAndroidthe other controls provided. Otherwise it will causeclassnotfoundexceptionexception.

the layout with the button corresponding Click event in 3.0 The following versions do not function.

The following is a simple demonstration of the custom music playback notification (not set setongoing resident):

Layout in message.xml ( custom layout ) :

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3android:orientation= "Horizontal" android:layout_width= "Match_parent"4android:layout_height= "Wrap_content"5android:gravity= "Center" >6<ImageView7Android:id= "@+id/iv"8Android:layout_width= "Wrap_content"9android:layout_height= "Wrap_content"Tenandroid:src= "@mipmap/ic_launcher"/> One<TextView AAndroid:id= "@+id/tv" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" theandroid:layout_weight= "1" -android:gravity= "Center" -Android:text= "Similarities Walk the Tianya"/> -<Button +Android:id= "@+id/btn1" -Android:layout_width= "Wrap_content" +android:layout_height= "Wrap_content" Aandroid:text= "Play"/> at<Button -Android:id= "@+id/btn2" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -android:text= "Next"/> -</LinearLayout>

mainactivity corresponding Java implementation of the Code Mainactivity.java ( Show only click events ) :

1   Public voidShow2 (View v) {2Notificationcompat.builder Builder =NewNotificationcompat.builder ( This);3 Builder.setsmallicon (R.mipmap.guojia);4Remoteviews RV =Newremoteviews (Getpackagename (), r.layout.message);5Rv.settextviewtext (r.id.tv, "foam");//Modify the name of a song in a custom view6         //Modify a picture in a custom view (two methods)7 //Rv.setimageviewresource (r.id.iv,r.mipmap.ic_launcher);8 Rv.setimageviewbitmap (R.id.iv, Bitmapfactory.decoderesource (Getresources (), r.mipmap.ic_launcher));9 builder.setcontent (RV);TenNotification Notification =builder.build (); OneNotificationmanager Notificationmanager =(Notificationmanager) Getsystemservice (context.notification_service); A notificationmanager.notify (no_2,notification); -}

At this point notification related knowledge points are finished, thank you for your attention, good night

RELATED links:

use notification in Android to implement the general notification bar (notification example one)use notification in Android to implement a wide view notification bar (notification Example II)use notification to implement progress notification bar in Android (example three)

Android uses notification to implement notification management and custom notification bars (example four)

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.