Android自定义状态栏通知(Status Notification)的正确实现

在Android应用开发中,常常会使用到状态栏通知(Status Notification),例如新浪微博、网易新闻等提供的推送消息,软件后台更新时进度的显示等等,以下图所示: android

看了网上不少关于Notification的博客文章,发现几乎没有一个能将自定义状态栏通知彻底实现正确的,所以,本文就来讲说实现自定义状态栏通知常常被忽略的一些知识点。 app


1) 使用Notification最多见的场景 布局

运行在后台的Service当须要和用户交互时,因为它不能直接启动一个Activity,所以,Service必须使用Notification来间接的启动Activity(当用户点击Notification时跳转)。 字体


2) 自定义布局文件支持的控件类型 spa

Notification的自定义布局是RemoteViews,所以,它仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件,同时支持AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些UI控件。对于其余不支持的控件,使用时将会抛出ClassNotFoundException异常。 xml


3) Notification支持的Intent类型(都是PendingIntent类的实例) 事件

contentIntent:在通知窗口区域,Notification被单击时的响应事件由该intent触发; ip

deleteIntent:在通知窗口区域,当用户点击所有清除按钮时,响应该清除事件的Intent; utf-8

fullScreenIntent:响应紧急状态的全屏事件(例如来电事件),也就是说通知来的时候,跳过在通知区域点击通知这一步,直接执行fullScreenIntent表明的事件。 开发

上面三种PendingIntent能够拉起Activity、Service和BroadcastReceiver,如图所示:


4) 状态栏通知字体的设置

不一样的手机,不一样的Android平台版本,状态栏通知窗口的背景颜色可能千差万别,例如Android2.3以前的版本通知窗口默认背景是白色的,Android4.0以后的版本通知窗口背景默认是黑色的,这就须要在设置Notification的字体时加以区别,不然,很容易致使通知的字体颜色和背景色同样,从而看不到字体部分,市面上不少app就存在这个问题。以下图所示,华为智汇云和百度音乐这两款应用就明显存在这个问题。


从Android2.3(API level 9)开始,系统为默认通知栏布局的字体定义了样式以下:

"android:TextAppearance.StatusBar.EventContent"

"android:TextAppearance.StatusBar.EventContent.Title"

所以,在2.3以后的版本中咱们自定义布局文件中的字体直接应用这个样式就能够。对于2.3以前的版本,因为背景色是白色的,所以,咱们使用以下系统预约义样式指定字体的颜色:

?android:attr/textColorPrimary

所以,在res的values目录下定义styles.xml文件以下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <style name="NotificationText">  
  5.         <item name="android:textColor">?android:attr/textColorPrimary</item>  
  6.     </style>  
  7.   
  8.     <style name="NotificationTitle">  
  9.         <item name="android:textColor">?android:attr/textColorPrimary</item>  
  10.         <item name="android:textStyle">bold</item>  
  11.     </style>  
  12.   
  13. </resources>  

在res的values-v9目录下定义styles.xml文件以下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />  
  5.   
  6.     <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />  
  7.   
  8. </resources>  

自定义通知布局文件使用styles文件以下:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/layout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     tools:ignore="ContentDescription" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/image"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_centerVertical="true"  
  14.         android:layout_marginLeft="5.0dp"  
  15.         android:layout_marginRight="10.0dp" />  
  16.   
  17.     <RelativeLayout  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_centerVertical="true"  
  21.         android:layout_toRightOf="@id/image" >  
  22.   
  23.         <TextView  
  24.             android:id="@+id/title"  
  25.             style="@style/NotificationTitle"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content" />  
  28.   
  29.         <TextView  
  30.             android:id="@+id/text"  
  31.             style="@style/NotificationText"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content"  
  34.             android:layout_below="@id/title" />  
  35.     </RelativeLayout>  
  36.   
  37. </RelativeLayout>
相关文章
相关标签/搜索