notification是一种出现在任务栏的提示,特别是在4.0以后notification改进了不少,本文内容都是基于4.0及4.1以后总结来的
分类
notification有以下几种:
1>普通notification
1.内容标题
2.大图标
3.内容
4.内容附加信息
5.小图标
6.时间
2>大布局Notification
大布局notification是在android4.1以后才增加的,大布局notification与小布局notification只在**‘7'**部分有区别,其它部分都一致。大布局notification只有在所有notification的最上 面时才会显示大布局,其它情况下显示小布局。你也可以用手指将其扩展为大布局(前提是它是大布局)。如下图:
大布局notification有三种类型:如图1为NotificationCompat.InboxStyle 类型。图2左部为NotificationCompat.BigTextStyle。图2右部 为:NotificationCompat.BigPictureStyle
** 3>自定义布局notification**
除了系统提供的notification,我们也可以自定义notification。如下图所示的一个音乐播放器控制notification:
**如何创建notification
**
**** 1>实例化一个NotificationCompat.Builder对象;如builder
2>调用builder的相关方法对notification进行上面提到的各种设置
****3>调用builder.build()方法此方法返回一个notification对象。
4>实例化一个
NotificationManager对象;如:manager
5>调用manager的notify方法。
注:
一个notification不必对上面所有的选项都进行设置,但有3项是必须的:
小图标, set by setSmallIcon()
内容标题, set by setContentTitle()
内容, set by setContentText()
示例代码
示例程序截图:
**0>初始化部分代码
**
复制代码代码如下:
                   NOTIFICATION_ID_1    
         NOTIFICATION_ID_2    
         NOTIFICATION_ID_3    
         NOTIFICATION_ID_4    
         NOTIFICATION_ID_5    
         NOTIFICATION_ID_6    
         NOTIFICATION_ID_7    
         NOTIFICATION_ID_8    
        messageNum    
       context    
       manager  
       icon  
         btns     idbtn1 idbtn2  
             idbtn3 idbtn4 idbtn5 idbtn6 idbtn7 idbtn8  
             idbtn9   
       
        savedInstanceState   
         savedInstanceState  
         layoutactivity_main  
           
       
          
         
         manager   NOTIFICATION_SERVICE  
         
           btn  btns   
             btn  
           
         icon    
                 drawableic_launcher  
       
       
        v   
          v   
          idbtn1  
               
               
          idbtn2  
               
               
          idbtn3  
               
               
          idbtn4  
               
               
          idbtn5  
               
               
          idbtn6  
               
               
          idbtn7  
               
               
          idbtn8  
               
               
          idbtn9  
               
               
           
             context  LENGTH_SHORT  
               
           
       
          
         manager  
       
**1>普通notification
**
复制代码代码如下:
                    notification   context  
                 icondrawableic_launcher  
                   
                   
                 messageNum  
                 DEFAULT_ALL  
                   
         managerNOTIFICATION_ID_1 notification  
       
**2>大布局Text类型notification
**
复制代码代码如下:
                textStyle     
         textStyle  
                   
                   
                   
                           
          notification   context  
                 icondrawableic_launcher  
                   
                   
                 textStyle  
                 DEFAULT_ALL  
                   
         managerNOTIFICATION_ID_2 notification  
       
**3> 大布局Picture类型notificatio
**
代码如下:
                pictureStyle     
         pictureStyle  
                 icon  
          notification   context  
                 icondrawableic_launcher  
                   
                   
                 pictureStyle  
                 DEFAULT_ALL  
                   
         managerNOTIFICATION_ID_3 notification  
       
**4>大布局Inbox类型notification
**
代码如下:
                inboxStyle     
         inboxStyle  
                   
           i   i   i  
             inboxStyle  i  
          notification   context  
                 icondrawableic_launcher  
                   
                   
                 inboxStyle  
                 DEFAULT_ALL  
                   
         managerNOTIFICATION_ID_4 notification  
       
**5>自定义notification
**
效果图:
并对中间的播放按钮做了一个简单的点击处理事件(点击播放后,请关闭幕帘否则可能会看不到toast提示)
代码如下:
                remoteViews     
                 layoutcustom_notification  
          intent      
          pendingIntent  context   
                 intent   
         remoteViewsidpaly_pause_music  
                 pendingIntent  
          builder   context  
         builderremoteViewsdrawablemusic_icon  
                 icon  
                   
         managerNOTIFICATION_ID_8 builder  
       
布局文件:
代码如下:
 xml version encoding    xmlnsandroid  
     androidlayout_width  
     androidlayout_height  
     androidgravity  
     androidorientation   
       
         androidid  
         androidlayout_width  
         androidlayout_height  
         androidsrc   
       
         androidlayout_width  
         androidlayout_height  
         androidgravity  
         androidorientation   
           
             androidid  
             androidlayout_width  
             androidlayout_height  
             androidlayout_weight  
             androidsrc   
           
             androidid  
             androidlayout_width  
             androidlayout_height  
             androidlayout_weight  
             androidsrc   
           
             androidid  
             androidlayout_width  
             androidlayout_height  
             androidlayout_weight  
             androidsrc   
       
   
带进度条的notification
代码如下:
                     builder     
                 context  
         buildericondrawableic_launcher  
                   
                   
                   
              
               
                  
                  progress    
                  progress   progress   progress     
                     
                     builder progress   
                     managerNOTIFICATION_ID_7 builder  
                        
                         
                          \   
                        e   
                         out  
                       
                   
                 builder  
                             
                 managerNOTIFICATION_ID_7 builder  
               
           
       
**点击事件处理 
**
有时候我们可能需要实现这样的功能:当新notification出现时,我们希望点击它后可直接进入应用相应的界面中去完整查看或处理此消息的功能。然后,当我们点击back按钮时返回到应用主界面而不是桌面。比如:当我们有新的短信来时,我们在任务栏中点击它后进入读信息页面,当我们读完短信后,按“返回”键回到短信的主界面,而不是手机桌面。要实现这样的功能要我们做相应的处理:
**1>返回应用主界面
**
代码如下:
                    stackBuilder    
         
         stackBuilder  
         
          resultIntent      
         stackBuilderresultIntent  
         
          resultPendingIntent  stackBuilder  
                 FLAG_UPDATE_CURRENT  
          notification   context  
                 icondrawableic_launcher  
                   
                   
                 resultPendingIntent  
                 DEFAULT_ALL  
         managerNOTIFICATION_ID_5 notification  
           
       
并需要我们在配置文件中对我们用来显示详细信息的OtherActivity进行相应的配置如下:
代码如下:
               
           
2>直接返回桌面
有些时候我们可能需要实现这样的功能:当我们点击notification时弹出一个稍大点的窗口来显示整个消息,这窗口的作用就是用来显示整个消息内容的,和此应用内的其它Activity都没有关系,然后当我们点击"back"后直接返回到手机桌面。要实现这样的功能我们只需要调用builder的.setContentIntent方法,然后对所要跳转到的activity在配置文件中进行一些配置:
代码如下:
                    notification   context  
                 icondrawableic_launcher  
                   
                   
                 messageNum  
                 DEFAULT_ALL  
                   
         managerNOTIFICATION_ID_1 notification  
       
0配置文件:
代码如下:
                    notification   context  
                 icondrawableic_launcher  
                   
                   
                 messageNum  
                 DEFAULT_ALL  
                   
         managerNOTIFICATION_ID_1 notification  
       
1注:
notification必须设置小图标,否则通知无法显示








		
		
		

还没有评论,来说两句吧...