转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51074832   文章出自:[薛瑄的博客](http://blog.csdn.net/xx326664162)

你也可以查看我的其他同类文章,也会让你有一定的收货!

我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?Notification就是解决这个问题的。

Android系统也是在不断升级的,有关Notification的用法也就有很多种,有的方法已经被android抛弃了,现在我实现了三种不同的方法,并适应不同的android版本。

先看效果图:

Notification类中的一些常量,字段,方法简单介绍一下

1、NotificationManager常用字段:

contentIntent   设置PendingIntent对象,点击时发送该Intent
defaults        添加默认效果(提示声音、闪光、震动等)
flags           设置flag位(是否可被清除等)
icon            设置图标
sound           设置声音
tickerText      显示在状态栏中的文字
when            发送此通知的时间戳

defaults变量可选值:

DEFAULT_ALL     使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS  使用默认闪光提示
DEFAULT_SOUNDS  使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动
【说明】:加入手机震动,一定要在manifest.xml中加入权限:
<uses-permission android:name="android.permission.VIBRATE" />

以上的效果常量可以叠加

notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE;
notification.defaults |= DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)

flag变量可选值:

FLAG_AUTO_CANCEL   该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR      该通知不能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT     是否一直进行,比如音乐一直播放,知道用户响应

2、NotificationManager常用方法介绍:

public void cancelAll()     移除所有通知(只是针对当前Context下的Notification)
public  void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public  void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为id
public  void notify(int id, Notification notification) 将通知加入状态栏,标记为id

主要的代码如下:

package net.loonggg.notification;  import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;  public class MainActivity extends Activity {  private static final int NOTIFICATION_FLAG = 1;  @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  }  public void notificationMethod(View view) {  // 获取系统的通知服务,它是一个系统Service。
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  switch (view.getId()) {  // 第一种:默认通知(Android 6.0已经废弃setLatestEventInfo)case R.id.btn1:  // 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,  new Intent(this, MainActivity.class), 0);  // 下面需兼容Android 2.x版本是的处理方式  // Notification notify1 = new Notification(R.drawable.message,  // "TickerText:" + "您有新短消息,请注意查收!", System.currentTimeMillis());  Notification notify1 = new Notification();  notify1.icon = R.drawable.message;  notify1.tickerText = "TickerText:您有新短消息,请注意查收!";  notify1.when = System.currentTimeMillis();  notify1.setLatestEventInfo(this, "Notification Title",  "This is the notification message", pendingIntent);  notify1.number = 1;  notify1.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。  // 通过通知管理器来发起通知。如果id不同(NOTIFICATION_FLAG),则显示不同的通知条。id相同的,则覆盖manager.notify(NOTIFICATION_FLAG, notify1);  break;  // 默认通知 API11及之后可用  case R.id.btn2:  PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,  new Intent(this, MainActivity.class), 0);  // 通过Notification.Builder来创建通知,注意API Level  // API11之后才支持  Notification notify2 = new Notification.Builder(this)  .setSmallIcon(R.drawable.message) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap   icon)  .setTicker("TickerText:" + "您有新短消息,请注意查收!")// 设置在status bar上显示的提示文字  .setContentTitle("Notification Title")// 设置在下拉status bar后Activity,本例子中的NotififyMessage的TextView中显示的标题  .setContentText("This is the notification message")// TextView中显示的详细内容  .setContentIntent(pendingIntent2) // 关联PendingIntent  .setNumber(1) // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。  .getNotification(); // 注意在API level 16及之后使用build()来代替该函数,在API11中可以使用getNotificatin()notify2.flags |= Notification.FLAG_AUTO_CANCEL;  manager.notify(NOTIFICATION_FLAG, notify2);  break;  // 默认通知 API16及之后可用  case R.id.btn3:  PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,  new Intent(this, MainActivity.class), 0);  // 通过Notification.Builder来创建通知,注意API Level  // API16之后才支持  Notification notify3 = new Notification.Builder(this)  .setSmallIcon(R.drawable.message)  .setTicker("TickerText:" + "您有新短消息,请注意查收!")  .setContentTitle("Notification Title")  .setContentText("This is the notification message")  .setContentIntent(pendingIntent3).setNumber(1).build(); // 注意build()是在API level16及之后增加的,API11可以使用getNotificatin()来替代  notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。  manager.notify(NOTIFICATION_FLAG, notify3);// 通过通知管理器来发起通知。如果id不同(NOTIFICATION_FLAG),则显示不同的通知条。id相同的,则覆盖break;  // 自定义通知  case R.id.btn4:  // Notification myNotify = new Notification(R.drawable.message,  // "自定义通知:您有新短信息了,请注意查收!", System.currentTimeMillis());  Notification myNotify = new Notification();  myNotify.icon = R.drawable.message;  myNotify.tickerText = "TickerText:您有新短消息,请注意查收!";  myNotify.when = System.currentTimeMillis();  myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除  RemoteViews rv = new RemoteViews(getPackageName(),  R.layout.my_notification);  rv.setTextViewText(R.id.text_content, "hello wrold!");  myNotify.contentView = rv;  Intent intent = new Intent(Intent.ACTION_MAIN);  PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);  myNotify.contentIntent = contentIntent;  manager.notify(NOTIFICATION_FLAG, myNotify);  break;  case R.id.btn5:  // 清除id为NOTIFICATION_FLAG的通知  manager.cancel(NOTIFICATION_FLAG);  // 清除所有的通知  // manager.cancelAll();  break;  default:  break;  }  }
}  

API16 之后的使用.build()创建Notification对象 ,也可以使用下面的这种方式先创建Builder 对象,在使用.build()创建Notification对象

public void simpleNotice(View view) {  //此Builder为android.support.v4.app.NotificationCompat.Builder中的,下同。  Builder mBuilder = new Builder(this);  //系统收到通知时,通知栏上面显示的文字。  mBuilder.setTicker("天津,晴,2~15度,微风");  //显示在通知栏上的小图标  mBuilder.setSmallIcon(R.drawable.consult_answer);  //通知标题  mBuilder.setContentTitle("天气预报");  //通知内容  mBuilder.setContentText("天津,晴,2~15度,微风");  //设置大图标,即通知条上左侧的图片(如果只设置了小图标,则此处会显示小图标)  mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.share_sina));  //显示在小图标左侧的数字  mBuilder.setNumber(6);  //设置为不可清除模式  mBuilder.setOngoing(true);  //显示通知,id必须不重复,否则新的通知会覆盖旧的通知(利用这一特性,可以对通知进行更新)  nm.notify(1, mBuilder.build());  }  

主布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context=".MainActivity" >  <Button  android:id="@+id/btn1"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:onClick="notificationMethod"  android:text="默认通知(已被抛弃,但是通用)" />  <Button  android:id="@+id/btn2"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:onClick="notificationMethod"  android:text="默认通知(API11之后可用)" />  <Button  android:id="@+id/btn3"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:onClick="notificationMethod"  android:text="默认通知(API16之后可用)" />  <Button  android:id="@+id/btn4"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:onClick="notificationMethod"  android:text="自定义通知" />  <Button  android:id="@+id/btn5"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:onClick="notificationMethod"  android:text="清除通知" />  </LinearLayout>  

自定义通知的布局文件my_notification.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:background="#ffffff"  android:orientation="vertical" >  <TextView  android:id="@+id/text_content"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:textSize="20sp" />  </LinearLayout> 

一些经验:

如果两个ID相同Notification对象的A和B,在下拉状态栏中,A是点击消失,B是点击不消失的,这样的话,点击A也不会消失。

参考:

http://blog.csdn.net/loongggdroid/article/details/17616509/ 
http://blog.csdn.net/books1958/article/details/41207435 
http://www.oschina.net/code/snippet_270292_14489 
http://www.cnblogs.com/error404/archive/2011/08/18/2144913.html

http://blog.csdn.net/feng88724/article/details/6259071

Android之Notification的多种用法相关推荐

  1. android notification 的总结分析,Android Notification的多种用法总结

    android notification的多种用法总结 我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今 ...

  2. Android关于notification的在不同API下的用法说明

    当我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们 ...

  3. android notification 的总结分析,Android中Notification用法实例总结

    本文实例总结了 Android中Notification用法.分享给大家供大家参考,具体如下: 我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图 ...

  4. 带你走进Android之Notification

    我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也 ...

  5. android网络技术-WebView的用法

    有时候我们可能会碰到一些比较特殊的需要,比如需要在应用程序里展示一些网页,加载和显示网页通畅都是浏览器的任务,但是需求里又明确指出,不允许打开系统浏览器,这个时候我们就要使用到WebView控件. 下 ...

  6. Android 通知栏Notification

    Android 通知栏Notification 在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态 ...

  7. Android之Notification制作多媒体控制器

    上一篇讲述了Notification的基础用法,本篇将介绍,自定义通知栏,并利用讲到的内容,实现一个简单的音乐播发器. 1.自定义通知的实现: Notification有一个contentView属性 ...

  8. android 继承listview,Android listView 继承ListActivity的用法

    Android listView 继承ListActivity的用法 在手机中经常有列表方式.如果Activity中只有唯⼀⼀个List(这也是通常的情况),可以继 承ListActivity来实现. ...

  9. Android的notification通知

    概括: Android使用notification实现消息提醒,提供通知栏和通知抽屉查看通知和通知详情,Android的notification机制应用非常广泛,是指在应用程序之外显示的消息,布局由系 ...

最新文章

  1. Could not GET “XXXXX.xml”Received status code 400
  2. java 开源记账_生鲜配送系统ERP(JAVA开源版)-水产记账ipad
  3. 各种优秀依赖库,大赞
  4. 流浪宠物救助网站前端页面_全国爱心人士齐聚鞍山 救助流浪猫狗
  5. np.c_和np.r_的用法解析
  6. Netty中的Future
  7. 查看GCC编译优化的具体选项开关
  8. python房价数据分析波士顿_Python编程数据科学入门 - 波士顿房价数据分析
  9. 如何像“二次元萌妹”小冰一样写诗创作和即兴编曲?
  10. 千锋锋云智慧联合百度重磅打造人工智能产业学院建设方案
  11. [小雨学编程之C#] 18.1 数据类型转换,数据类型别名
  12. keepalived的双机热备(主从模式)-主机宕机备机无法替代踩坑
  13. python数据分析04--双均线策略、金叉与死叉的理解
  14. 电子烟能破壳类四大天王“和大天壹”新物种么?
  15. 练习题12-外观数列
  16. 关于MybatisPlus的QueryWrapper定义查询条件的and()和or()方法连用问题
  17. Java volatile 关键字的作用
  18. 乐字节最全面向对象深入1
  19. 使用ArcGIS API和Three.js在三维场景中实现动态立体墙效果
  20. log4cpp源码阅读:Appender组件学习

热门文章

  1. php打印j机1015的驱动,HP LaserJet1015打印机驱动
  2. 迷宫求解【穷举求解法】
  3. ios重签名shell脚本_03:双层签名和重签名流程以及shell脚本重签名
  4. 任正非接班人亮相:原来他要的是这种类型!
  5. 俄罗斯一家庭与世隔绝40年 不知有二战(图)
  6. 【论文笔记】DSIFN:用于高分辨率双时相遥感图像变化检测的深度监督图像融合网络
  7. [论文评析]基于人体姿态识别的立定跳远 动作智能评估系统
  8. 举个栗子!Tableau 技巧(4):如何做空心饼图(环形图)
  9. ft232rl可以了
  10. 关于edu邮箱收不到国外投稿邮件的解决办法