zoukankan      html  css  js  c++  java
  • 各个版本中Notification对象创建的方法

    前几天在4.0.3平台也就是API Level 15中,使用Notification的setLatestEventInfo()函数时,显示成setLatestEventInfo()效果,查看文档发现,在API Level 11中,该函数已经被替代,不推荐使用了。

            经过一番查询和折腾,发现在不同的版本下Notification使用有一些不同,涉及到改成Builder的使用,现在网上大多数资料还是API Level 11版本前的用法介绍,如果不熟悉的话,会绕一些弯路。

            现在总结如下,希望对以后使用的程序员有所帮助。

    1、低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。

    1 <span style="font-size:14px;">Intent  intent = new Intent(this,MainActivity);  
    2 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
    3         notification.setLatestEventInfo(context, title, message, pendingIntent);          
    4         manager.notify(id, notification);  
    5 </span><span style="font-size:14px;"></span>
    View Code

    2、高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。

    此时,前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置

    1 Notification.Builder builder = new Notification.Builder(context)  
    2             .setAutoCancel(true)  
    3             .setContentTitle("title")  
    4             .setContentText("describe")  
    5             .setContentIntent(pendingIntent)  
    6             .setSmallIcon(R.drawable.ic_launcher)  
    7             .setWhen(System.currentTimeMillis())  
    8             .setOngoing(true);  
    9 notification=builder.getNotification(); 
    View Code

    3、高于API Level 16的版本,就可以用Builder和build()函数来配套的方便使用notification了。

    1 Notification notification = new Notification.Builder(context)    
    2          .setAutoCancel(true)    
    3          .setContentTitle("title")    
    4          .setContentText("describe")    
    5          .setContentIntent(pendingIntent)    
    6          .setSmallIcon(R.drawable.ic_launcher)    
    7          .setWhen(System.currentTimeMillis())    
    8          .build();
    View Code

      Tips:

    在构造notification的时候有很多种写法,但是要注意,用

    Notification notification = new Notification();

    这种构建方法的时候,一定要加上notification.icon这个设置,不然,程序虽然不会报错,但是会没有效果

  • 相关阅读:
    SQLite Select语句的意外发现
    和一个经理人谈话的经典语句
    [转]如何动态增长一个数组的大小
    [转]Spring AOP中文教程
    为Wildfish框架增加方法调用日志[Aspectsharp]
    第四周学习心得
    《大道至简:软件工程实践者的思想》观后感
    第三周学习心得
    暑假第一周Java学习心得
    第二周学习心得
  • 原文地址:https://www.cnblogs.com/hot-destiny/p/5746756.html
Copyright © 2011-2022 走看看