项目环境

Project Build Target:Android 6.0

问题:

使用 new Notification(int icon, CharSequence tickerText, long when)构造函数时,Eclipse却提示:" The constructor Notification(int, CharSequence, long) is deprecated "

源码如下:

 /**
 * Constructs a Notification object with the information needed to
 * have a status bar icon without the standard expanded view.
 *
 * @param icon The resource id of the icon to put in the status bar.
 * @param tickerText The text that flows by in the status bar when the notification first
 * activates.
 * @param when The time to show in the time field. In the System.currentTimeMillis
 * timebase.
 *
 * @deprecated Use {@link Builder} instead.
 */
 @Deprecated
 public Notification(int icon, CharSequence tickerText, long when)
 {
   this.icon = icon;
   this.tickerText = tickerText;
   this.when = when;
 }

  在不同的版本下Notification使用有一些不同,涉及到Builder的使用。现在总结如下,希望对以后使用的程序员有所帮助。

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

 Intent  intent = new Intent(this,MainActivity);
 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
 notification.setLatestEventInfo(context, title, message, pendingIntent);
 manager.notify(id, notification); 

  高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。

 Notification.Builder builder = new Notification.Builder(context)
             .setAutoCancel(true)
             .setContentTitle("title")
             .setContentText("describe")
             .setContentIntent(pendingIntent)
             .setSmallIcon(R.drawable.ic_launcher)
             .setWhen(System.currentTimeMillis())
             .setOngoing(true);
 notification=builder.getNotification();

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

 Notification notification = new Notification.Builder(context)
          .setAutoCancel(true)
          .setContentTitle("title")
          .setContentText("describe")
          .setContentIntent(pendingIntent)
          .setSmallIcon(R.drawable.ic_launcher)
          .setWhen(System.currentTimeMillis())
          .build();

【注意点】:
    在构造notification的时候有很多种写法,但是要注意,用
  Notification notification = new Notification();
  这种构建方法的时候,一定要加上notification.icon这个设置,不然,程序虽然不会报错,但是会没有效果。

问题:

使用了Notification下的setLatestEventInfo()方法时,Eclipse却提示:“ The method setLatestEventInfo(Context, String, String, PendingIntent) is undefined for the type Notification”!

查看源码:

 /**
 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
 * layout.
 *
 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
 * in the view.</p>
 * @param context The context for your application / activity.
 * @param contentTitle The title that goes in the expanded entry.
 * @param contentText The text that goes in the expanded entry.
 * @param contentIntent The intent to launch when the user clicks the expanded notification.
 * If this is an activity, it must include the
 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
 * that you take care of task management as described in the
 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
 * Stack</a> document.
 *
 * @deprecated Use {@link Builder} instead.
 * @removed
 */
 @Deprecated

 public void setLatestEventInfo(Context context,
 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
 Notification.Builder builder = new Notification.Builder(context);

 // First, ensure that key pieces of information that may have been set directly
 // are preserved
 builder.setWhen(this.when);
 builder.setSmallIcon(this.icon);
 builder.setPriority(this.priority);
 builder.setTicker(this.tickerText);
 builder.setNumber(this.number);
 builder.setColor(this.color);
 builder.mFlags = this.flags;
 builder.setSound(this.sound, this.audioStreamType);
 builder.setDefaults(this.defaults);
 builder.setVibrate(this.vibrate);
 builder.setDeleteIntent(this.deleteIntent);

 // now apply the latestEventInfo fields
 if (contentTitle != null) {
 builder.setContentTitle(contentTitle);
 }
 if (contentText != null) {
 builder.setContentText(contentText);
 }
 builder.setContentIntent(contentIntent);
 builder.buildInto(this);
 }

setLatestEventInfo方法已被removed。

Android不同版本下Notification创建方法的更多相关文章

  1. 【转】【Android】Android不同版本下Notification创建方法

    使用 new Notification(int icon, CharSequence tickerText, long when)构造函数时,Eclipse却提示:" The constru ...

  2. opencv直线检测在c#、Android和ios下的实现方法

    opencv直线检测在c#.Android和ios下的实现方法 本文为作者原创,未经允许,不得转载 :原文由作者发表在博客园:http://www.cnblogs.com/panxiaochun/p/ ...

  3. Android不刷机下的app2sd方法(dex cache占空间解决篇)

    抱着5年的HTC G7这个古董,一直没有想法去换换. 近期微信.支付宝什么的apk应用都開始走程序巨型化,一次性就来个50MB的空间占用,让还是Android 2.2的手机怎样吃的消? 看看100多M ...

  4. Ubuntu下编译Android JNI最靠谱的方法...

    网上资料太杂乱,搞了大半天都还是没搞懂怎么系统的调用NDK.最后干脆放弃了Win改用Ubuntu编译JNI,虽然编译环境简单了,但是资料却少了不少.几乎没有一篇完整的文章.我想或许是能在Ubuntu下 ...

  5. ubuntu10.04版本下android源码的编译

    首先是网址:http://software.intel.com/en-us/blogs/2012/03/06/hands-on-notesbuild-android-x86-ics-4-virtual ...

  6. Delphi XE7下如何创建一个Android模拟器调试

    利用Delphi XE7我们可以进行多种设备程序的开发,尤其是移动开发应用程序得到不断地加强.在实际的Android移动程序开发中,如果我们直接用android真机直接调试是非常不错.一是速度快,二是 ...

  7. Android实现系统下拉栏的消息提示——Notification

    Android实现系统下拉栏的消息提示--Notification 系统默认样式 默认通知(通用) 效果图 按钮 <Button android:layout_width="match ...

  8. java高版本下各种JNDI Bypass方法复现

    目录 0 前言 1 Java高版本JNDI绕过的源代码分析 1.1 思路一的源码分析 1.2 思路二的源码分析 2 基于本地工厂类的利用方法 2.1 org.apache.naming.factory ...

  9. Linux下查看内核、CPU、内存及各组件版本的命令和方法

    Linux下查看内核.CPU.内存及各组件版本的命令和方法 Linux查看内核版本: uname -a                        more /etc/*release       ...

随机推荐

  1. 算法笔记_041:寻找和为定值的多个数(Java)

    目录 1 问题描述 2 解决方案 1 问题描述 输入两个整数n和sum,要求从数列1,2,3,...,n中随意取出几个数,使得它们的和等于sum,请将其中所有可能的组合列出来. 2 解决方案 上述问题 ...

  2. volist 自增序号 分页如何实现?

    TP框架模板中如何生成自增数据 {$_GET['p']*10-10+$i} /* 分页序号计算    */ function addnum($k,$num){     return ($k +1 ) ...

  3. 机器学习数学基础- gradient descent算法(上)

    为什么要了解点数学基础 学习大数据分布式计算时多少会涉及到机器学习的算法,所以理解一些机器学习基础,有助于理解大数据分布式计算系统(比如spark)的设计.机器学习中一个常见的就是gradient d ...

  4. HTML 事件属性(如:onsubmit)

    学习啥都需要一个基础 基础很重要: HTML 4 的新特性之一是可以使 HTML 事件触发浏览器中的行为,比方说当用户点击某个 HTML 元素时启动一段 JavaScript. 在现代浏览器中都内置有 ...

  5. POS配置

    1.配置 (1) 房产局 (2) 银联端 2.运行 (1)   房产端 右击BizMAPSSP项目---Run As—run congfigurations—Java Application—选择Bi ...

  6. LibRec:一个实现推荐系统的Java库包

    LibRec是一个用于实现推系统 RS 的Java库包,实现推荐系统的两个经典问题: rating prediction(评分排行预测) 和 item ranking (项目排行),其内置了经典的机器 ...

  7. lamp 一键安装

    下载安装(ssh登录服务器,执行如下操作即可,需要用到root用户权限来安装) 源码编译安装 wget http://dl.wdlinux.cn:5180/lanmp_laster.tar.gz ta ...

  8. 网页中PNG透明背景图片的完美应用

    PNG 图片在网站设计中是不可或缺的部分,最大的特点应该在于 PNG 可以无损压缩,而且还可以设置透明,对于增强网站的图片色彩效果有重要的作用. 但为什么 PNG 图片却没有 GIF 和 JPG 图片 ...

  9. django abstract base class ---- 抽象基类

    抽象蕨类用于定义一些同享的列.类本身并不会在数据库端有表与之对应 一.例子: 1.定义一个叫Person 的抽象基类.Student 继承自Person from django.db import m ...

  10. Windows里配置Apache2.2+PHP5.3+mod_fcgid运行高效的FastCGI模式

    日发现win平台的Apache也可以跑FastCGI模式了,就折腾了一会.FastCGI模式是经实践证明了比传统的模块模式运行效率高很多.记录如下: 一.mod_fcgid配置说明 1.首先下载mod ...