Android types use notification to implement notification management and custom notification bar instances (example IV) _android

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 the use of new notifications, and you should consider some of the values of the notification bar before updating to alert the user. For example, our mobile phone's SMS system, when there are new news, our notice bar is only to change the number of text messages, rather than a separate notice for each message bar for tips.

Modify Notification

You can set up a notification, of course, to update a notification, and we update it by the ID used when calling notificationmanager.notify (ID, notification). To update your previous announcement, 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 previously. 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 notice, showing the summary:

(Note that the notification number accumulates and the notification bar disappears after clicking the notification)

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

Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import Android.content.Context;
Import android.content.Intent;
Import Android.graphics.BitmapFactory;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.support.v7.app.NotificationCompat;
Import Android.view.View;
Import Android.widget.RemoteViews;
  public class Mainactivity extends Appcompatactivity {private static final int no_1 = 0x1; int num =1;//Initial notification quantity is 1 @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstances
    Tate);
  Setcontentview (R.layout.activity_main); //button click event (notification bar) public void Show1 (View v) {notificationcompat.builder Builder = new Notificationcompat.builder (thi
    s);
    Builder.setsmallicon (R.mipmap.ic_launcher);
    Builder.setcontenttitle ("New News");
    Builder.setcontenttext ("You have a new piece of news");
    Builder.setnumber (num++); Set Click notification Jump page, notify disappears Builder.setautocancel (true);
    Intent Intent = new Intent (this,main2activity.class);
    pendingintent pi = pendingintent.getactivity (this, 0, intent, pendingintent.flag_update_current);
    Builder.setcontentintent (PI);
    Notification Notification = Builder.build ();
    Notificationmanager Notificationmanager = (notificationmanager) getsystemservice (Context.notification_service);
  Notificationmanager.notify (no_1,notification);
 }
}

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

Example two: Customizing the notification bar

The notification framework allows you to customize the layout of notifications. Define the appearance of the notification by remoteviews the object. Custom notification layouts are similar to regular notifications, but they are based on remoteviews objects that are defined in an XML file.

The available height of a custom notification depends on the notification view. The normal view layout height is limited to 64DP, and the layout of an expandable view is limited to 256DP.

To define your own notification layout, start by fetching an instance of an Remoteviews object from the extended XML file. Then, like calling the Setcontenttitle () method, we need to invoke setcontent (). In order to be able 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 notifications. You can use any name you want, but the suffix must be. Xml.

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

The Remoteviews object also contains methods to add chronometer and ProgressBar to you. For more information about customizing the layout of a notification bar, refer to Remoteviews's documentation.

Note: When you use a custom notification bar, pay special attention to how your customized notification bar works in different directions and resolution devices. Of course, this recommendation is important for all view layouts. However, it is particularly important for the notification bar because the control of the notification drawer is very limited. Don't make your own notice bar too complex to make sure it's flexible.

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

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

Attention:

The custom layout for notification is remoteviews, like other remoteviews, in a custom view layout file that supports only framelayout, LinearLayout, Relativelayout three layout controls and AnalogClock, Chronometer, Button, ImageButton, ImageView, ProgressBar, TextView, Viewflipper, ListView, GridView, StackView, and Adapterviewflipper display controls that do not support subclasses of these classes or other controls provided by Android. Otherwise, it can cause classnotfoundexception exceptions.

The layout of the button with the corresponding Click event in the following version of 3.0 does not function.

The following is a simple demo of the custom music playback notification (no setongoing resident):

Layout message.xml (custom layout):

 <?xml version= "1.0" encoding= "Utf-8"?> "<linearlayout xmlns:android=" http:// Schemas.android.com/apk/res/android "android:orientation=" horizontal "android:layout_width=" Match_parent "Android : layout_height= "wrap_content" android:gravity= "center" > <imageview android:id= "@+id/iv" Android:layout_w
    Idth= "Wrap_content" android:layout_height= "wrap_content" android:src= "@mipmap/ic_launcher"/> <TextView Android:id= "@+id/tv" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_ weight= "1" android:gravity= "center" android:text= "Shangjian go Tianya"/> <button android:id= "@+id/btn1" Android: Layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Play"/> <Button 
D= "@+id/btn2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Next song"/> </LinearLayout> 

Mainactivity corresponds to the Java implementation Code Mainactivity.java (only demo click event):

public void Show2 (View v) {
    Notificationcompat.builder Builder = new Notificationcompat.builder (this);
    Builder.setsmallicon (R.mipmap.guojia);
    Remoteviews RV = new Remoteviews (Getpackagename (), r.layout.message);
    Rv.settextviewtext (r.id.tv, "bubble"); Modify the song name in the Custom view
    //Modify the picture in the custom view (two methods)
//    Rv.setimageviewresource ( R.id.iv,r.mipmap.ic_launcher);
    Rv.setimageviewbitmap (R.id.iv, Bitmapfactory.decoderesource (Getresources (), r.mipmap.ic_launcher));
    Builder.setcontent (RV);
    Notification Notification = Builder.build ();
    Notificationmanager Notificationmanager = (notificationmanager) getsystemservice (context.notification_service);
    Notificationmanager.notify (no_2,notification);
  }

So far notification related knowledge points summed up, the above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.