状态通知栏

在android的应用层中,涉及到不少应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等。html

下面就来讲说常常会使用到通知机制中的通知栏框架(Notificaiton),它适用于交互事件的通知。它是位于顶层能够展开的通知列表。它会时不时的提醒你什么软件该更新了,什么人发你微信消息了等。java

(网上看了下,全面介绍的文章很少,因此就萌生了写这篇的念头,随便看成回顾笔记。下面我就经过官方文档、源代码、书上的一些资料汇总下这一块的知识,并经过一个通知栏的汇总DEMO让你们更好的了解这个类的使用,内容有点多,能够根据需求看目录学习)。android


Notificaiton状态通知栏:git


功能做用


1.显示接收到短消息、即便消息等信息 (如QQ、微信、新浪、短信)  
2.显示客户端的推送消息(若有新版本发布,广告,推荐新闻等) 
3.显示正在进行的事物(例如:后台运行的程序)(如音乐播放器、版本更新时候的下载进度等)
github


思惟导图结构


思惟导图的大致结构(按照各个节点延伸拓展学习)微信

Notificaiton -- service   -- BroadcastReceiver  -- Intent(flag、Action等属性应用) --  PendingIntent
网络

感慨:app

一个Notificaiton通知的拓展使用就要涉及与4大组建的配合,因此学好总体的知识体系。
框架

联系:ide

1.因为service 是在后台运行,因此它意图作什么咱们看不到,能够经过Notificaiton 来显示提醒(如音乐的后台播放)。

2.service服务和BroadcastReceiver广播相结合,在加上Notificaiton 显示(如程序的后台更新)。

3.Intent做为意图处理,和Notificaiton的点击时间紧密结合在了一块儿,而且与BroadcastReceiver和service的联系也紧密不能够分割。

service 在后台以后经过BroadcastReceiver来通知Notificaiton 显示相关东西,在经过Intent完成用户的意图操做)

相关文档:Activity启动模式 及 Intent Flags 与 栈 的关联分析


对应的官方连接

 


设计文档 :   
使用教程 :  

开发文档 :http://developer.android.com/reference/android/app/Notification.html


大致了解


Notification支持文字内容显示、震动三色灯铃声等多种提示形式,在默认状况下,Notification仅显示消息标题消息内容送达时间这3项内容。如下就是通知的基本布局。

 

普通视图:

高度64dp

大试图的通知在展开前也显示为普通视图

元素:

 

1. 标题   Title/Name

2大图标  Icon/Photo

3. 内容文字   

4. 内容信息   MESSAGE

5. 小图标 Secondary Icon

6. 通知的时间 Timestamp,默认为系统发出通知的时间,也可经过setWhen()来设置



相关分析


状态通知栏主要涉及到2个类:  Notification 和 NotificationManager 

Notification为通知信息类,它里面对应了通知栏的各个属性

NotificationManager :  是状态栏通知的管理类,负责发通知、清除通知等操做。

注意:NotificationManager 是一个系统Service,因此必须经过 getSystemService(NOTIFICATION_SERVICE)方法来获取,方法以下。

[java] view plain copy 在CODE上查看代码片派生到个人代码片
  1. NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  


使用步骤:


流程模块:

第一步:

建立一个通知栏的Builder构造类  (Create a Notification Builder)

第二步:

定义通知栏的Action  (Define the Notification's Action)

第三步:

设置通知栏点击事件    (Set the Notification's Click Behavior)

第四步:

通知   (Issue the Notification)


代码模块:


实现系统默认的通知栏效果:

第一步:获取状态通知栏管理:

 
  1. NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  


第二步:实例化通知栏构造器NotificationCompat.Builder:

 
  1. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);  


第三步:对Builder进行配置:

 

 
  1. mBuilder.setContentTitle("测试标题")//设置通知栏标题  
  2.     .setContentText("测试内容") /<span style="font-family: Arial;">/设置通知栏显示内容</span>  
  3.     .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图  
  4. //  .setNumber(number) //设置通知集合的数量  
  5.     .setTicker("测试通知来啦") //通知首次出如今通知栏,带上升动画效果的  
  6.     .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,通常是系统获取到的时间  
  7.     .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级  
  8. //  .setAutoCancel(true)//设置这个标志当用户单击面板就可让通知将自动取消    
  9.     .setOngoing(false)//ture,设置他为一个正在进行的通知。他们一般是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,所以占用设备(如一个文件下载,同步操做,主动网络链接)  
  10.     .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,能够组合  
  11.     //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission  
  12.     .setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON  


对应的各个方法的属性(部分方法以上代码中已经做注释,就再也不介绍):

(1)方法:设置提醒标志符Flags

功能:提醒标志符,向通知添加声音、闪灯和振动效果等设置达到通知提醒效果,能够组合多个属性

有2种设置方法:

1.实例化通知栏以后经过给他添加.flags属性赋值。

  1. Notification notification = mBuilder.build();  
  2. notification.flags = Notification.FLAG_AUTO_CANCEL;  

2.经过setContentIntent(PendingIntent intent)方法中的意图设置对应的flags

  1. public PendingIntent getDefalutIntent(int flags){  
  2.     PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);  
  3.     return pendingIntent;  
  4. }  

提醒标志符成员:

Notification.FLAG_SHOW_LIGHTS              //三色灯提醒,在使用三色灯提醒时候必须加该标志符

Notification.FLAG_ONGOING_EVENT          //发起正在运行事件(活动中)

Notification.FLAG_INSISTENT   //让声音、振动无限循环,直到用户响应 (取消或者打开)

Notification.FLAG_ONLY_ALERT_ONCE  //发起Notification后,铃声和震动均只执行一次

Notification.FLAG_AUTO_CANCEL      //用户单击通知后自动消失

Notification.FLAG_NO_CLEAR          //只有所有清除时,Notification才会清除 ,不清楚该通知(QQ的通知没法清除,就是用的这个)

Notification.FLAG_FOREGROUND_SERVICE    //表示正在运行的服务


(2)方法:.setDefaults(int defaults)     (NotificationCompat.Builder中的方法,用于提示)


功能:向通知添加声音、闪灯和振动效果的最简单、使用默认(defaults)属性,能够组合多个属性(和方法1中提示效果同样的)

对应属性:

Notification.DEFAULT_VIBRATE    //添加默认震动提醒  须要 VIBRATE permission

Notification.DEFAULT_SOUND    // 添加默认声音提醒

Notification.DEFAULT_LIGHTS// 添加默认三色灯提醒

Notification.DEFAULT_ALL// 添加默认以上3种所有提醒


(3)方法:setVibrate(long[] pattern)


功能:设置震动方式。

使用:

 

  1. .setVibrate(new long[] {0,300,500,700});  

实现效果:延迟0ms,而后振动300ms,在延迟500ms,接着在振动700ms。

以上方法的还有种写法是

  1. mBuilder.build().vibrate = new long[] {0,300,500,700};  

以此类推,2种写法均可以。

若是但愿设置默认振动方式,设置了方法(2)中默认为DEFAULT_VIBRATE 便可。


(4)方法:.setLights(intledARGB ,intledOnMS ,intledOffMS )


功能:android支持三色灯提醒,这个方法就是设置不一样场景下的不一样颜色的灯。

描述:其中ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间。

注意:1)只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持三色灯提醒。

 

          2)这边的颜色跟设备有关,不是全部的颜色均可以,要看具体设备。


使用:

 

  1. .setLights(0xff0000ff, 300, 0)  

同理,如下方法也能够设置一样效果:

 

  1. Notification notify = mBuilder.build();  
  2. notify.flags = Notification.FLAG_SHOW_LIGHTS;  
  3. notify.ledARGB = 0xff0000ff;  
  4. notify.ledOnMS = 300;  
  5. notify.ledOffMS = 300;  

若是但愿使用默认的三色灯提醒,设置了方法(2)中默认为DEFAULT_LIGHTS便可。

(5)方法:.setSound(Uri sound)


功能:设置默认或则自定义的铃声,来提醒。

 

  1. //获取默认铃声  
  2. .setDefaults(Notification.DEFAULT_SOUND)  
  3. //获取自定义铃声  
  4. .setSound(Uri.parse("file:///sdcard/xx/xx.mp3"))  
  5. //获取Android多媒体库内的铃声  
  6. .setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5"))  

同理相同效果的另外一种设置方法这边就不讲, 和上面的都是同样的。


(6)方法:.setPriority(int pri)


功能:设置优先级

对应优先级描述以下图:

 

优先级 用户
MAX 重要而紧急的通知,通知用户这个事件是时间上紧迫的或者须要当即处理的。
HIGH 高优先级用于重要的通讯内容,例如短消息或者聊天,这些都是对用户来讲比较有兴趣的。
DEFAULT 默认优先级用于没有特殊优先级分类的通知。
LOW 低优先级能够通知用户但又不是很紧急的事件。
MIN 用于后台消息 (例如天气或者位置信息)。最低优先级通知将只在状态栏显示图标,只有用户下拉通知抽屉才能看到内容。

对应属性(做用看上图就可知道):

 

Notification.PRIORITY_DEFAULT

Notification.PRIORITY_HIGH

Notification.PRIORITY_LOW

Notification.PRIORITY_MAX

Notification.PRIORITY_MIN


 

(7)方法:setOngoing(boolean ongoing)


功能:设置为ture,表示它为一个正在进行的通知。他们一般是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,所以占用设备(如一个文件下载,同步操做,主动网络链接)


(8)方法:setProgress(int max, int progress,boolean indeterminate)


属性:max:进度条最大数值  、progress:当前进度、indeterminate:表示进度是否不肯定,true为不肯定,以下第3幅图所示  ,false为肯定下第1幅图所示

功能:设置带进度条的通知,能够在下载中使用

效果图以下:

       注意:此方法在4.0及之后版本才有用,若是为使用:若是为肯定的进度条:调用setProgress(max, progress, false)来设置通知,在更新进度的时候在此发起通知更新progress,而且在下载完成后要移除进度条,经过调用setProgress(0, 0, false)既可。

操做结束时,调用setProgress(0, 0, false)并更新通知以移除指示条



第四步:设置通知栏PendingIntent(点击动做事件等都包含在这里)

在第三步中,没有提到一个方法,就是setContentIntent(PendingIntent intent)这个方法,这里拿到这里讲。



 

知识点

1)什么是PendingIntent


PendingIntent和Intent略有不一样,它能够设置执行次数,主要用于远程服务通讯、闹铃、通知、启动器、短信中,在通常状况下用的比较少。


2)PendingIntent什么用


Notification支持多种Intent来响应单击事件、消除事件、处理紧急状态的全屏事件等。

这里就用到了setContentIntent(PendingIntent intent)来处理以上这么多的事件。


3)相关属性和方法

属性:

PendingIntent的位标识符:

FLAG_ONE_SHOT   表示返回的PendingIntent仅能执行一次,执行完后自动取消

FLAG_NO_CREATE     表示若是描述的PendingIntent不存在,并不建立相应的PendingIntent,而是返回NULL

FLAG_CANCEL_CURRENT      表示相应的PendingIntent已经存在,则取消前者,而后建立新的PendingIntent,这个有利于数据保持为最新的,能够用于即时通讯的通讯场景

FLAG_UPDATE_CURRENT     表示更新的PendingIntent


方法:


能够看出,它支持多种相应方式,有Activity、Broadcast、Service,就根据你自身需求去选择。


在各类状况下状况下它还会根据各类状况出发效果:

 

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

deleteIntent:当用户点击所有清除按钮时,响应该清除事件的Intent;

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


例如:在执行了点击通知以后要跳转到指定的XXX的Activity的时候,能够设置如下方法来相应点击事件:

  1. Intent intent = new Intent(context,XXX.class);  
  2. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);  
  3. mBuilder.setContentIntent(pendingIntent)  


例如:在执行了清空所有的通知操做时候,能够设置如下方法来相应这个事件:

采用setDeleteIntent(PendingIntent intent)方法或按照如下写法

  1. Intent deleteIntent = new Intent();  
  2. deleteIntent.setClass(context, XXXReceiver.class);  
  3. deleteIntent.setAction(DELETE_ACTION);  
  4. notification.deleteIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, 0);  


例如:在响应紧急事件(如来电)时候,能够设置如下方法来相应这个事件:

采用setFullScreenIntent(PendingIntent intent, boolean highPriority)


第五步,最简单的一部,发送通知请求

 

  1. mNotificationManager.notify(notifyId, mBuilder.build());  或者mNotificationManager.notify(notifyId, mBuilder.getNotification());



拓展


实现自定义的通知栏效果:

这里要用到RemoteViews这个类。实现如下2种自定义布局。


注意:

Notification的自定义布局是RemoteViews,和其余RemoteViews同样,在自定义视图布局文件中,仅 支持FrameLayout、LinearLayout、RelativeLayout三种布局控件和AnalogClock、Chronometer、 Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、 GridView、StackView和AdapterViewFlipper这些显示控件,不支持这些类的子类或Android提供的其余控件。不然会 引发ClassNotFoundException异常

 


步骤以下:

1)建立自定义视图

2)获取远程视图对象(注:Notification的contentView不能为空)

3)设置PendingIntent(来响应各类事件)

4)发起Notification

大致4步骤这里就不详细说了,下面就把DEMO中的列子拿出来讲下


样式:

1.自定义带按钮通知栏(以下样式)

 

“正在进行的”通知使用户了解正在运行的后台进程。例如,音乐播放器能够显示正在播放的音乐。也能够用来显示须要长时间处理的操做,例以下载或编码视频。“正在进行的”通知不能被手动删除。

 

 




实现方法以下:
 
  1. /** 
  2.      * 带按钮的通知栏 
  3.      */  
  4.     public void showButtonNotify(){  
  5.         NotificationCompat.Builder mBuilder = new Builder(this);  
  6.         RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.view_custom_button);  
  7.         mRemoteViews.setImageViewResource(R.id.custom_song_icon, R.drawable.sing_icon);  
  8.         //API3.0 以上的时候显示按钮,不然消失  
  9.         mRemoteViews.setTextViewText(R.id.tv_custom_song_singer, "周杰伦");  
  10.         mRemoteViews.setTextViewText(R.id.tv_custom_song_name, "七里香");  
  11.         //若是版本号低于(3。0),那么不显示按钮  
  12.         if(BaseTools.getSystemVersion() <= 9){  
  13.             mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.GONE);  
  14.         }else{  
  15.             mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.VISIBLE);  
  16.         }  
  17.         //  
  18.         if(isPlay){  
  19.             mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_pause);  
  20.         }else{  
  21.             mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_play);  
  22.         }  
  23.         //点击的事件处理  
  24.         Intent buttonIntent = new Intent(ACTION_BUTTON);  
  25.         /* 上一首按钮 */  
  26.         buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID);  
  27.         //这里加了广播,所及INTENT的必须用getBroadcast方法  
  28.         PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
  29.         mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev, intent_prev);  
  30.         /* 播放/暂停  按钮 */  
  31.         buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);  
  32.         PendingIntent intent_paly = PendingIntent.getBroadcast(this, 2, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
  33.         mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play, intent_paly);  
  34.         /* 下一首 按钮  */  
  35.         buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID);  
  36.         PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
  37.         mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next, intent_next);  
  38.           
  39.         mBuilder.setContent(mRemoteViews)  
  40.                 .setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT))  
  41.                 .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示  
  42.                 .setTicker("正在播放")  
  43.                 .setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级  
  44.                 .setOngoing(true)  
  45.                 .setSmallIcon(R.drawable.sing_icon);  
  46.         Notification notify = mBuilder.build();  
  47.         notify.flags = Notification.FLAG_ONGOING_EVENT;  
  48.         mNotificationManager.notify(notifyId, notify);  
  49.     }  
注意:带按钮的布局相应点击事件在3.0如下版本没有用,因此这边做了系统版本判断,来显示消失按钮。


2.自定义不带按钮通知栏


实现方法以下:
 
  1.         //先设定RemoteViews  
  2.         RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.view_custom);  
  3.         //设置对应IMAGEVIEW的ID的资源图片  
  4.         view_custom.setImageViewResource(R.id.custom_icon, R.drawable.icon);  
  5. //      view_custom.setInt(R.id.custom_icon,"setBackgroundResource",R.drawable.icon);  
  6.         view_custom.setTextViewText(R.id.tv_custom_title, "今日头条");  
  7.         view_custom.setTextViewText(R.id.tv_custom_content, "金州勇士官方宣布球队已经解雇了主帅马克-杰克逊,随后宣布了最后的结果。");  


以后调用:
  1. <span style="font-family: Arial, Helvetica, sans-serif;">mBuilder.setContent(view_custom)</span>  
来设定自定义的这个布局。



实现:大视图风格通知(注: 只在通知被展开时显示

 

什么时候展开:通知处在顶端,或者用户经过收拾展开

收件箱风格的通知:

 

相比普通视图,只多出:7. 详情区域


效果图以下:


详情区域根据用途可有多种风格:

1.NotificationCompat.BigPictureStyle 大图片风格:详情区域包含一个256dp高度的位图

2.NotificationCompat.BigTextStyle 大文字风格:显示一个大的文字块

3.NotificationCompat.InboxStyle  收件箱风格:显示多行文字  


各类风格都具备如下常规视图不具备的内容选项:

1.大标题:在展开视图时替代普通视图的标记

2.总结文字:容许你在详情区域之下增长一行内容


拿收件箱风格为例,实现代码以下:
  1.         NotificationCompat.BigPictureStyle inboxStyle = new NotificationCompat.InboxStyle();  
  2.         String[] events = new String[5];  
  3.         // Sets a title for the Inbox style big view  
  4.         inboxStyle.setBigContentTitle("大视图内容:");  
  5.         // Moves events into the big view  
  6.         for (int i=0; i < events.length; i++) {  
  7.             inboxStyle.addLine(events[i]);  
  8.         }  
  9.         mBuilder.setContentTitle("测试标题")  
  10.                 .setContentText("测试内容")  
  11. //              .setNumber(number)//显示数量  
  12.                 .setStyle(inboxStyle)//设置风格  
  13.                 .setTicker("测试通知来啦");  



开发中碰到的问题

注:下面所指的低版本是指2.3及2.3如下版本)

1.如何取消掉通知栏上的通知

  (1)设置对应的flags,让用户点击既被消除:

notification.flags = FLAG_AUTO_CANCEL;

    (2) 经过手动消除某项或则所有通知

mNotificationMgr.cancle(NOTIFICATION_ID);//消除对应ID的通知

mNotificationMgr.cancleAll();//消除建立的全部通知


2.低版本中的部分方法已经被弃用的

 (1)Notification.Builder(this).getNotification()

 (2)mNotification.setLatestEventInfo(this, "title", "content", null);  

这些方法都已经被启用,虽然还有效果,但是不建议使用。因此开发过程当中尽可能使用NotificationCompat.Builder(this)的构建方法去建立一个通知类。


3.低版本中会报的错误及解决方案:

解决方案:若是在高版本不会出错,而在2.3上面报了这个错误,经过开发文档中的如下知道你能够找打:

Activity in your app, and you should always start that Activity when users click the notification. To do this, use the setContentIntent() method.

设置contentIntent,若是你点击没有意图,能够在赋值的的Intent中设置为new Intent()既可,切记contentIntent不能为空。

代码以下:

 

  1. public PendingIntent getDefalutIntent(int flags){  
  2.     PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);  
  3.     return pendingIntent;  
  4. }  


(2)错误代 码:android.app.RemoteServiceException: Bad notification posted from package com.example.notifications: Couldn't expand RemoteViews for: StatusBarNotification(package=com.example.notifications id=101 tag=null notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x2))

解决方法:


4.低版本中,自定义的通知栏中若是带有按钮,可能按钮点击事件会失灵

解决方法:看其它的应用,好像在低版本都会隐藏掉那些按钮,就是为了避免影响用户体验,因此应该就这么解决,判断版本号在去决定是否如今按钮。


5.低版本中,自定义布局中的字体颜色看不清

如右图:

解决方案:

因为2.3及以前版本,背景设是白色的那咱们定义字体颜色为系统预设的颜色:

?android:attr/textColorPrimary

在资源的src/values目录中的style.xml文件中设置它标题和内容的样式为:

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>    
  3.     
  4.     <style name="NotificationContent">    
  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>  


在2.3以后的版本中(即API >=9的版本中),在资源文件下的src/values-v9目录中的style.xml文件中设置它标题和内容的样式为:

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>    
  3.     
  4.     <style name="NotificationContent" parent="android:TextAppearance.StatusBar.EventContent" />    
  5.     
  6.     <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />    
  7.     
  8. </resources>    


最后赋给自定义布局中的对应标题和内容对应的style便可。

对应解决网址:

1.http://stackoverflow.com/questions/6250356/how-to-use-default-notification-style

2.http://stackoverflow.com/questions/4867338/custom-notification-layouts-and-text-colors/7320604#7320604

3.http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView   (官方文档)

http://developer.android.com/about/versions/android-2.2-highlights.html


6.低版本中mBuilder.setProgress(100, progress, false);没用,不显示进度条

解决方法:此方法在4.0及之后版本才有用,若是为早期版本:须要自定义通知布局,其中包含ProgressBar视图



7.自定义布局的时候,不一样版本方法不同。(弄了半天,在2.3版本不显示,原来是方法不兼容)


2.3及2.3以前:

经过

  1. Notification notify = mBuilder.build();  
  2. notify.contentView = view_custom;  
  3. mNotificationManager.notify(notifyId, notify)  

方法赋予VIEW。

2.3以后:

经过Builder如下方法赋于自定义布局。

mBuilder.setContent(view_custom)



这里就不贴DEMO中的代码了,你们能够下个DEMO本身看,里面也都有注释的,可能有的地方会有错误,忘你们指出,以便及时修改,谢谢。


一个DEMO让你更懂Notification

DEMO截图:

          


DEMO下载:下载地址

相关文章
相关标签/搜索