使用 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。

原文地址:http://www.cnblogs.com/Arture/p/5523695.html

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

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

    项目环境 Project Build Target:Android 6.0 问题: 使用 new Notification(int icon, CharSequence tickerText, lon ...

  2. Android 各API版本代码常量

    Android开发中常常有版本比较这个功能,来做版本兼容或者,其他相关效果等   以下是Android 7.0版本下的源码:   public static class VERSION_CODES { ...

  3. Android新旧版本Notification

    Android新旧版本Notification 在notification.setLatestEventInfo() 过时了 以前: NotificationManager mn = (Notific ...

  4. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  5. 【转】Android Studio-1.2版本设置教程

    如果重新安装Android Studio的话要重新配置风格选项啥的,这篇是个很好的教程,原文链接:http://blog.csdn.net/skykingf/article/details/45485 ...

  6. 第十二章:Android数据存储(下)

    一.SQLite介绍 提到数据存储问题,数据库是不得不提的.数据库是用来存储关系型数据的不二利器.Android为开发者提供了强大的数据库支持,可以用来轻松地构造基于数据库的应用.Android的数据 ...

  7. Android笔记——Windows环境下Android Studio v1.0安装教程

    本文主要讲解Windows环境下Android Studio的安装教程,Mac的Android Studio安装与此类似不在赘述,另外友情提示Windows下的SDK与Mac的SDK是通用的,可以直接 ...

  8. Android数据库升级、降级、创建(onCreate() onUpgrade() onDowngrade())[4]

    数据库版本升级对软件的管理操作. 我们手机经常会收到xxx软件升级什么的提醒,你的软件版本更新,同时你的数据库对应的版本也要相应的更新. 数据库版本更新需要主要的问题: 软件的1.0版本升级到1.1版 ...

  9. 使用Gradle构建Android应用内测版本

    在开发应用的过程中,有时候需要比较当前线上版本和正在开发中的版本差异,目前的做法只能是在两个不同的设备上面安装线上版本和开发中的版本,因为当前版本在调试过程中会覆盖旧版本.本文通过使用gradle来构 ...

随机推荐

  1. du和df命令的区别

    du和df命令都被用于获得文件系统大小的信息:df用于报告文件系统的总块数及剩余块数,du -s /<filesystem>用于报告文件系统使用的块数.但是,我们可以发现从df命令算出的文 ...

  2. tomcat在debug模式启动直接提示:弹框无法启动,无报错信息;但直接启动的话,就会有报错信息

    今天运行项目,Debug模式启动Tomcat,直接弹框:无法启动(翻译,因为后来整理,所以都忘记当时的截图) 后来尝试直接start,发现不弹框了,但是console有报出错信息. 类似以下错误 20 ...

  3. [转]关于几本模拟IC设计书

    1.P.R.Gray的书 这本书被业界誉为模拟IC的Bible,盛名之下,必无虚士.现在已经出到第四版,作者无一例外是业界大牛,该书论述严谨,思路清晰,对电路分析透彻,定义严格明确,无愧Bible之名 ...

  4. Linux-Ubuntu操作记录

    ubuntu 基本命令 1.帮助man      --help简单的帮助  如:man tar 或者tar --help      help command 较详细的帮助      man comma ...

  5. Windows上怎么安装ELK

    In this guide I will show that it is also possible to run Logstash on a Windows Server 2012 machine ...

  6. 深入理解Docker Volume(一)

    想要了解Docker Volume,首先我们需要知道Docker的文件系统是如何工作的.Docker镜像是由多个文件系统(只读层)叠加而成.当我们启动一个容器的时候,Docker会加载镜像层并在其上添 ...

  7. Test for Required Behavior, Not Incidental Behavior

    Test for Required Behavior, Not Incidental Behavior Kevlin Henney A COMMON PITFALL IN TESTING is to ...

  8. 在windows下codeblocks中配置pthread库

    转自:http://blog.csdn.net/u013172314/article/details/50846198 如果添加方法不正确,可能会出现pthread_create’未定义的引用,所以下 ...

  9. 使用自定义的按钮替换默认的<input type='file'>

    可以通过让默认的input type = 'file'按钮透明度变为0,并且让它刚好覆盖在自定义的按钮上,来实现此效果: 将它写成一个jQuery插件: (function($){ $.fn.brow ...

  10. C++ test的使用

    http://www.parasoft.com/jsp/trial_request.jsp?itemId=303 去下载,原来是个商业的测试软件,还要去购买,这个成本太大了.. http://down ...