下面来谈谈notification,这个notification一般用在电话,短 信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个快讯。已添加的 Notification.Builder,使其更容易构建通知。notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。 它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。

先来区分以下状态栏和状态条的区别:

1、状态条就是手机屏幕最上方的一个条形状的区域;

在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;

2、状态栏就是手从状态条滑下来的可以伸缩的view;

在状态栏中一般有两类(使用FLAG_标记):

(1)正在进行的程序;

(2)是通知事件;

快速创建一个Notification的步骤简单可以分为以下四步:

第一步:通过getSystemService()方法得到NotificationManager对象;

1.nManager = (NotificationManager) this.getSystemService(service);

第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;

01.notification.icon = R.drawable.ic_launcher;// 设置通知的图标 
02.notification.tickerText = tickerText; // 显示在状态栏中的文字 
03.notification.when = when; // 设置来通知时的时间 
04.notification.sound = Uri.parse("android.resource://com.sun.alex/raw/dida"); // 自定义声音 
05.notification.flags = Notification.FLAG_NO_CLEAR; // 点击清除按钮时就会清除消息通知,但是点击通知栏的通知时不会消失 
06.notification.flags = Notification.FLAG_ONGOING_EVENT; // 点击清除按钮不会清除消息通知,可以用来表示在正在运行 
07.notification.flags |= Notification.FLAG_AUTO_CANCEL; // 点击清除按钮或点击通知后会自动消失 
08.notification.flags |= Notification.FLAG_INSISTENT; // 一直进行,比如音乐一直播放,知道用户响应 
09.notification.defaults = Notification.DEFAULT_SOUND; // 调用系统自带声音 
10.notification.defaults = Notification.DEFAULT_VIBRATE;// 设置默认震动 
11.notification.defaults = Notification.DEFAULT_ALL; // 设置铃声震动 
12.notification.defaults = Notification.DEFAULT_ALL; // 把所有的属性设置成默认

第三步:通过NotificationManager对象的notify()方法来执行一个notification的消息;

1.nManager.notify(ID, notification);

第四步:通过NotificationManager对象的cancel()方法来取消一个notificatioin的消息;

1.nManager.cancel(ID);

Notification.build构造Notification方法介绍:

void setLatestEventInfo(Context context , CharSequencecontentTitle,CharSequence  contentText,PendingIntent contentIntent)

功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象

参数:    context              上下文环境

contentTitle       状态栏中的大标题

contentText       状态栏中的小标题

contentIntent    点击后将发送PendingIntent对象

说明:要是在Notification中加入图标,在状态栏和状态条中显示图标一定要用这个方法,否则报错!

NotificationManager类的常用方法:

通过获取系统服务来获取该对象:

NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE) ;

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

01.package com.sun.alex; 
02. 
03.import android.app.Activity; 
04.import android.app.Notification; 
05.import android.app.NotificationManager; 
06.import android.app.PendingIntent; 
07.import android.content.Intent; 
08.import android.net.Uri; 
09.import android.os.Bundle; 
10.import android.view.View; 
11.import android.view.View.OnClickListener; 
12.import android.widget.Button; 
13. 
14.public class NotificationActivity extends Activity { 
15. 
16.private Button sendBtn, cancelBtn; 
17.private Notification notification; 
18.private NotificationManager nManager; 
19.private Intent intent; 
20.private PendingIntent pIntent; 
21.// Notification的标示ID 
22.private static final int ID = 1
23. 
24.@Override 
25.public void onCreate(Bundle savedInstanceState) { 
26.super.onCreate(savedInstanceState); 
27.setContentView(R.layout.main); 
28. 
29.sendBtn = (Button) this.findViewById(R.id.send); 
30.cancelBtn = (Button) this.findViewById(R.id.cancel); 
31. 
32.String service = NOTIFICATION_SERVICE; 
33.nManager = (NotificationManager) this.getSystemService(service); 
34. 
35.notification = new Notification(); 
36.String tickerText = "测试Notifaction"; // 通知提示 
37.// 显示时间 
38.long when = System.currentTimeMillis(); 
39. 
40.notification.icon = R.drawable.ic_launcher;// 设置通知的图标 
41.notification.tickerText = tickerText; // 显示在状态栏中的文字 
42.notification.when = when; // 设置来通知时的时间 
43.notification.sound = Uri.parse("android.resource://com.sun.alex/raw/dida"); // 自定义声音 
44.notification.flags = Notification.FLAG_NO_CLEAR; // 点击清除按钮时就会清除消息通知,但是点击通知栏的通知时不会消失 
45.notification.flags = Notification.FLAG_ONGOING_EVENT; // 点击清除按钮不会清除消息通知,可以用来表示在正在运行 
46.notification.flags |= Notification.FLAG_AUTO_CANCEL; // 点击清除按钮或点击通知后会自动消失 
47.notification.flags |= Notification.FLAG_INSISTENT; // 一直进行,比如音乐一直播放,知道用户响应 
48.notification.defaults = Notification.DEFAULT_SOUND; // 调用系统自带声音 
49.notification.defaults = Notification.DEFAULT_SOUND;// 设置默认铃声 
50.notification.defaults = Notification.DEFAULT_VIBRATE;// 设置默认震动 
51.notification.defaults = Notification.DEFAULT_ALL; // 设置铃声震动 
52.notification.defaults = Notification.DEFAULT_ALL; // 把所有的属性设置成默认 
53. 
54.sendBtn.setOnClickListener(sendClickListener); 
55.cancelBtn.setOnClickListener(cancelClickListener); 
56.
57. 
58.private OnClickListener sendClickListener = new OnClickListener() { 
59.@Override 
60.public void onClick(View v) { 
61.// 单击通知后会跳转到NotificationResult类 
62.intent = new Intent(NotificationActivity.this
63.NotificationResult.class); 
64.// 获取PendingIntent,点击时发送该Intent 
65.pIntent = PendingIntent.getActivity(NotificationActivity.this, 0
66.intent, 0); 
67.// 设置通知的标题和内容 
68.notification.setLatestEventInfo(NotificationActivity.this, "标题"
69."内容", pIntent); 
70.// 发出通知 
71.nManager.notify(ID, notification); 
72.
73.}; 
74. 
75.private OnClickListener cancelClickListener = new OnClickListener() { 
76.@Override 
77.public void onClick(View v) { 
78.// 取消通知 
79.nManager.cancel(ID); 
80.
81.}; 
82.}

Android中的消息通知(NotificationManager和Notification)的更多相关文章

  1. 浅析Android中的消息机制(转)

    原博客地址:http://blog.csdn.net/liuhe688/article/details/6407225 在分析Android消息机制之前,我们先来看一段代码: public class ...

  2. 浅析Android中的消息机制(转)

    在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...

  3. 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.

    在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...

  4. 浅析Android中的消息机制

    在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...

  5. 重温Android中的消息机制

    引入: 提到Android中的消息机制,大家应该都不陌生,我们在开发中不可避免的要和它打交道.从我们开发的角度来看,Handler是Android消息机制的上层接口.我们在平时的开发中只需要和Hand ...

  6. Android中的消息机制

    在分析Android消息机制之前.我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...

  7. 谈谈对Android中的消息机制的理解

    Android中的消息机制主要由Handler.MessageQueue.Looper三个类组成,他们的主要作用是 Handler负责发送.处理Message MessageQueue负责维护Mess ...

  8. MQTT协议实现Android中的消息收发

    前言 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输),基于发布/订阅范式的消息协议,是一种极其简单和轻量级的消息协议,专为受限设备和低带宽.高延迟 ...

  9. Android中的消息机制:Handler消息传递机制

    参考<疯狂android讲义>第2版3.5 P214 一.背景 出于性能优化考虑,Android的UI操作并不是线程安全的,这意味着如果有多个线程并发操作UI组件,可能导致线程安全问题.为 ...

随机推荐

  1. git的一些相关知识

    1.配置多个git远程仓库的ssh-Key切换(转自) 目前的git仓库如github都是通过使用SSH与客户端连接,如果只是固定使用单个git仓库的单个用户 (first),生成生成密钥对后,将公钥 ...

  2. drupal7 Views Slideshow 简单教程

    一.下载安装(略) 二.内容类型建立(过程略,首页幻灯),字段建立(过程略)主要有2个字段,图片字段 和 指向链接字段 三.view 1.建立一个新的view,名称为frontbanner 显示为内容 ...

  3. ubuntu tomcat 部署java web

    1,安装jdk apt-get install openjdk-7-jdk 2,下载tomcat.解压到对应的文件夹 3,将xxx.war放入到 tomcat下的  webapp目录下(此目录下不要有 ...

  4. Android实战技巧:深入解析AsyncTask

    AsyncTask的介绍及基本使用方法 关于AsyncTask的介绍和基本使用方法可以参考官方文档和Android实战技巧:多线程AsyncTask这里就不重复. AsyncTask引发的一个问题 上 ...

  5. How to Debug Enterprise Portal Code in Dynamics AX 2009

    转载 To set up debugging for pages1. Log into the server that is running the AOS.2. Open the Microsoft ...

  6. 多线程调用WebClient速度变慢的问题

    设置 System.Net.ServicePoint 对象所允许的最大并发连接数 System.Net.ServicePoint 对象允许的最大并发连接数.默认值为 2 System.Net.Serv ...

  7. 一个方法告诉你as3的removeChild性能有多烂

    其实as3这个坑我早就踩过,不过那时没太注意,最近把项目的removeChild干掉换成了visible=false,发现效率质的提升…… 先贴一下代码吧 private function testR ...

  8. C++ string 用法详解

    /////////////////////////////////////////////////////////////////////////////////// 任何人对本文进行引用都要标明作者 ...

  9. adb permission denied

    1.没有得到root权限,可以用刷机精灵一键root. 2.root不彻底,手机安装超级adbd,即可.

  10. VB6.0手册

    1.Form窗体事件 Private Sub Form_Activate() '焦点在此窗口时触发  MsgBox "窗体的Activate事件"  End Sub    Priv ...