The correct implementation of custom Status bar Notification for Android

Source: Internet
Author: User

In Android Application Development, Status Notification (Status Notification) is often used, such as push messages provided by Sina Weibo and Netease News, and the progress of updating the software background, as shown in:

 


I read a lot of blog articles about Notification on the Internet and found that almost no Notification on the custom status bar can be fully implemented correctly. Therefore, this article describes some knowledge points that are often ignored to implement custom status bar notifications.

 


1) The most common use cases of Notification

When a Service running in the background needs to interact with the user, because it cannot start an Activity directly, the Service must use Notification to indirectly start the Activity (jump when the user clicks Notification ).

 


2) controls supported by custom Layout files

The custom Layout of Notification is RemoteViews. Therefore, it only supports layout controls such as FrameLayout, LinearLayout, and RelativeLayout, it also supports AnalogClock, Chronometer, Button, ImageButton, ImageView, ProgressBar, TextView and other UI controls. For other unsupported controls, a ClassNotFoundException exception will be thrown during use.

 


3) Intent types supported by Notification (all are PendingIntent instances)

ContentIntent: In the Notification window area, the intent triggers the RESPONSE event when the Notification is clicked;

DeleteIntent: In the notification window, When you click the clear all button, the Intent that responds to the event is cleared;

FullScreenIntent: responds to emergency full-screen events (such as incoming call events). This means that when a notification is sent, you can skip the click notification step in the notification area and directly execute the event represented by fullScreenIntent.

The above three PendingIntent types can be used to pull Activity, Service, and BroadcastReceiver ,:

 

 


4) set the status bar notification font

Different mobile phones and different Android platforms may have different background colors in the status bar Notification window. For example, in versions earlier than Android, the default background of the notification window is white, in Versions later than Android, the background of the Notification window is black by default, which must be different when the Notification font is set. Otherwise, the color of the Notification is the same as that of the background color, as a result, the font cannot be seen. This problem exists in many apps on the market.

From Android2.3 (API level 9), the system defines the following la s for the default notification bar layout:

"Android: TextAppearance. StatusBar. EventContent"

"Android: TextAppearance. StatusBar. EventContent. Title"

Therefore, in Versions later than 2.3, we can apply this style directly to the fonts in the custom Layout file. For versions earlier than 2.3, because the background color is white, we use the following predefined system style to specify the font color:

? Android: attr/textColorPrimary

Therefore, the styles. xml file is defined in the res values directory as follows:

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
 
<Style name = "icationicationtext">
<Item name = "android: textColor">? Android: attr/textColorPrimary </item>
</Style>
 
<Style name = "NotificationTitle">
<Item name = "android: textColor">? Android: attr/textColorPrimary </item>
<Item name = "android: textStyle"> bold </item>
</Style>
 
</Resources>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>

<Style name = "icationicationtext">
<Item name = "android: textColor">? Android: attr/textColorPrimary </item>
</Style>

<Style name = "NotificationTitle">
<Item name = "android: textColor">? Android: attr/textColorPrimary </item>
<Item name = "android: textStyle"> bold </item>
</Style>

</Resources>

The styles. xml file is defined in the res values-v9 directory as follows:


[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
 
<Style name = "icationicationtext" parent = "android: TextAppearance. StatusBar. EventContent"/>
 
<Style name = "NotificationTitle" parent = "android: TextAppearance. StatusBar. EventContent. Title"/>
 
</Resources>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>

<Style name = "icationicationtext" parent = "android: TextAppearance. StatusBar. EventContent"/>

<Style name = "NotificationTitle" parent = "android: TextAppearance. StatusBar. EventContent. Title"/>

</Resources>
The styles file used in the Custom notification layout file is as follows:


[Html]
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/layout"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Tools: ignore = "ContentDescription">
 
<ImageView
Android: id = "@ + id/image"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_centerVertical = "true"
Android: layout_marginLeft = "5.0dp"
Android: layout_marginRight = "10.0dp"/>
 
<RelativeLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerVertical = "true"
Android: layout_toRightOf = "@ id/image">
 
<TextView
Android: id = "@ + id/title"
Style = "@ style/NotificationTitle"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
 
<TextView
Android: id = "@ + id/text"
Style = "@ style/icationicationtext"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ id/title"/>
</RelativeLayout>
 
</RelativeLayout>

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/layout"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Tools: ignore = "ContentDescription">

<ImageView
Android: id = "@ + id/image"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_centerVertical = "true"
Android: layout_marginLeft = "5.0dp"
Android: layout_marginRight = "10.0dp"/>

<RelativeLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerVertical = "true"
Android: layout_toRightOf = "@ id/image">

<TextView
Android: id = "@ + id/title"
Style = "@ style/NotificationTitle"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>

<TextView
Android: id = "@ + id/text"
Style = "@ style/icationicationtext"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ id/title"/>
</RelativeLayout>

</RelativeLayout>

Author: ACE1985
 

 

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.