转自:http://blog.csdn.net/songyachao/article/details/51245370#comments

今天使用4.0.3使用

Notification notification2 = new Notification(R.drawable.advise2,
"通知测试", System.currentTimeMillis());
notification2.setLatestEventInfo(getActivity(), "testTitle", "testContent", null);

结果androidstudio报错,setLatestEventInfo该方法找不到,经过查证官方在API Level 11中,该函数已经被替代,不推荐使用了。古在4.0.3平台也就是API Level 15中,使用Notification的setLatestEventInfo()函数时,显示setLatestEventInfo()效果。建议使用Notification.Builder来创建 notification 实例

 Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
builder1.setSmallIcon(R.drawable.advise2); //设置图标
builder1.setTicker("显示第二个通知");
builder1.setContentTitle("通知"); //设置标题
builder1.setContentText("点击查看详细内容"); //消息内容
builder1.setWhen(System.currentTimeMillis()); //发送时间
builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光
builder1.setAutoCancel(true);//打开程序后图标消失
Intent intent =new Intent (MainActivity.this,Center.class);
PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, , intent, );
builder1.setContentIntent(pendingIntent);
Notification notification1 = builder1.build();
notificationManager.notify(, notification1); // 通过通知管理器发送通知

如果该通知只是起到 “通知”的作用,不希望用户点击后有相应的跳转,那么,intent,pendingIntent这几行代码可以不写

 Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.advise);
builder.setTicker("显示第一个通知");
builder.setContentTitle("第一个通知");
builder.setContentText("每天进步一点点");
builder.setWhen(System.currentTimeMillis()); //发送时间
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
notificationManager.notify(, notification);

第一个具有点击提示有跳转功能,后面一个没有跳转功能,只是提示作用

以下借鉴其他博主的总结:

在不同的版本下Notification使用有一些不同,涉及到改成Builder的使用,现在网上大多数资料还是API Level 11版本前的用法介绍,如果不熟悉的话,会绕一些弯路。
    现在总结如下,希望对以后使用的程序员有所帮助。
    低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。

Intent  intent = new Intent(this,MainActivity);
PendingIntent pendingIntent = PendingIntent.getActivity(context, , 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这个设置,不然,程序虽然不会报错,但是会没有效果。

Androidn Notification的使用,解决找不到setLatestEventInfo方法的更多相关文章

  1. 没有R.java问题找不到getActionBar()方法

    android项目,可是项目中没有重要的R.java,并且报错,说是找不到getActionBar()方法,上网寻找原因,终于寻得解决方法:    1.解决项目中没有R.java问题.在Eclipse ...

  2. 解决找不到mkfs.ubifs命令

    解决找不到mkfs.ubifs命令 ubuntu 版本:14.04 sudo apt-get update sudo apt-get install mtd-utils sudo apt-get in ...

  3. ActionBarSherlock SlidingMenu整合,解决SlidingMenu example的getSupportActionBar()方法不能用问题

    今天下载了SlidingMenu来研究,发现里面那个自带的example不能使用,总是提示BaseActivity 里面找不到getSupportActionBar()方法,到Github上面一查果然 ...

  4. 错误 在类中找不到main方法请将main方法定义为 public static void main String args否则JavaFX应用程序类必须扩展javafx-ap

    最近在使用eclipse编写java程序时遇到这样一个问题: 错误在类中找不到main方法,请将main方法定义为 public static void main(String[] args)否则 J ...

  5. 错误: 在类中找不到 main 方法, 请将 main 方法定义为:public static void main(String[] args)否则 JavaFX 应用程序类必须扩展javafx.ap

    最近在使用eclipse编写java程序时遇到这样一个问题: 错误在类中找不到main方法,请将main方法定义为 public static void main(String[] args)否则 J ...

  6. 找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.应用程序类必 须扩展javafx.application.Application”

    用eclipse写代码的时候,写了一个简单的程序,编译的时候突然出现“错误: 在类 com.test.demo 中找不到 main 方法, 请将 main 方法定义为: public static v ...

  7. 【转】解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight)

    解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight) 转载自:http://www.360doc.com/content/13/1126/09/10504424_332211 ...

  8. 找不到 main 方法

    前几天打开了好久不用的eclipse,发现报了个奇怪的错误 ***中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) ...

  9. 原创博客>>>解决粘包问题的方法

    目录 原创博客>>>解决粘包问题的方法 原创博客>>>解决粘包问题的方法 服务端: import socket import struct service=sock ...

随机推荐

  1. XMPP 安装ejabberd 搭建服务器环境

    网上各种找..各种安装失败.. 终于.... ejabberd 下载列表.... http://www.process-one.net/en/ejabberd/archive/  建议下载old 版本 ...

  2. PHP文件锁定写入实例分享

    PHP文件锁定写入实例解析. 原文地址:http://www.jbxue.com/article/23118.html PHP文件写入方法,以应对多线程写入,具体代码: function file_w ...

  3. CCDictionary

    /** * CCDictionary is a class like NSDictionary in Obj-C . * * @note Only the pointer of CCObject or ...

  4. python __name__ = '__main__' 的作用

    很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...

  5. Redis List数据类型

    一.概述:      在Redis中,List类型是按照插入顺序排序的字符串链表.和数据结构中的普通链表一样,我们可以在其头部(left)和尾部(right)添加新的元素.在插入时,如果该键并不存在, ...

  6. 非nodejs方式的vue.js的使用

    1.node环境 详细见我之前的文章,node的安装 2.git环境 git bash命令窗,支持bash命令,cmd不支持bash命令 3.cnpm安装 cnpm是针对国内使用npm网络慢的而使用的 ...

  7. shell编程:for 循环

    hell 编程——for in 循环   -------for in 格式-------     for 无$变量 in 字符串 do $变量 done   一简单的字符串 枚举遍历法,利用for i ...

  8. 【转】cocos2d-x动画加速与减速

    移步原帖传送门:cocos2d-x动画加速与减速 动画是游戏的必然要素之一,在整个游戏过程中,又有着加速.减速动画的需求.以塔防为例子,布塔的时候希望能够将游戏减速,布好塔后,则希望能将游戏加速:当某 ...

  9. std::bind()图解

    参考:http://blog.think-async.com/2010/04/bind-illustrated.html 避免链接失效,就把文中图转过来了,这几张就清楚的说明了bind的用法和原理.

  10. pthread之线程堆栈

    转:http://blog.csdn.net/horstlinux/article/details/7666032 //先来讲说线程内存相关的东西,主要有下面几条: //进程中的所有的线程共享相同的地 ...