今天在学习Notification时同时参考了一些相关的博客,现在结合自身学习实际来总结一下。

在使用手机时,当有未接来电或者短消息时,通常会在手机屏幕上的状态栏上显示。而在Android中有提醒功能的也可以用AlertDialog,但是当使用AlertDialog 的时候,用户正在进行的操作将会被打断。因为当前焦点被AlertDialog得到。我们可以想像一下,当用户打游戏正爽的时候,这时候来了一条短信。如果这时候短信用AlertDialog提醒,用户必须先去处理这条提醒,从而才能继续游戏。用户可能会活活被气死。而使用Notification就不会带来这些麻烦事,用户完全可以打完游戏再去看这条短信。所以在开发中应根据实际需求,选择合适的控件。

状态栏通知涉及到两个类,一是Notification,它代表一个通知;另一个是NotificationManager,它是用于发送Notification的系统服务。

使用状态栏通知一般有4个步骤:

1、  通过getSystemService()方法获取NotificationManager服务。

2、  创建一个Notification对象,并为其设置各种属性。

3、  为Notification对象设置事件信息。

4、  通过NotificationManager类的notify()方法将通知发送到状态栏。

下面我们通过一个例子来实际操作一下:

首先是布局文件new_main.xml的使用:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:id="@+id/button1"
android:text="发送消息"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <Button
android:id="@+id/button2"
android:text="清空消息"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>

再创建一个Activity跳转后,新的Activity所需的页面content.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="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>

接着创建一个ContentActivity(点击通知后跳转到的页面):

 public class ContentActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
}
}

在MainActivity中定义两个变量:

final int NOTIFYID_1 = 123; // 第一个通知的ID
final int NOTIFYID_2 = 124; // 第二个通知的ID

然后重写onCreate()方法:

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_main);
//获取通知管理器,用于发送通知
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { Notification notify = new Notification();
notify.icon = R.drawable.ic_launcher;
notify.tickerText = "您有一个新的消息!";
notify.when = System.currentTimeMillis();//获取当前时间,返回的是毫秒
notify.defaults = Notification.DEFAULT_ALL;//设置默认的通知方式(声音、震动、闪光灯)
notify.setLatestEventInfo(MainActivity.this, "通知1",
"今天下午不上课啦!", null);
notificationManager.notify(NOTIFYID_1, notify);
Notification notify1 = new
Notification(R.drawable.ic_launcher, "又是一条短信",
System.currentTimeMillis());
Intent intent = new Intent(MainActivity.this,
ContentActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, intent, 0);
// Notification notify1 = new Notification.Builder(MainActivity.this)
// .setContentTitle("第二个通知 ")
// .setContentText("查看详情")
// .setSmallIcon(R.drawable.ic_launcher)
// .setContentIntent(pendingIntent).build();
notify1.flags |= Notification.FLAG_AUTO_CANCEL; notify1.setLatestEventInfo(MainActivity.this, "通知2", "点击查看详情",
pendingIntent);
notificationManager.notify(NOTIFYID_2, notify1);
}
}); Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// //清除ID号为常量NOTIFYID_1的通知
notificationManager.cancelAll(); // 清除全部通知 }
}); }

最后在清单文件中添加相关代码:

<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.VIBRATE" /> <activity android:name="com.topcsa.demo_notification.ContentActivity"
android:label="通知"
android:theme="@android:style/Theme.Dialog" />

好了,一个简单的Demo就做好了。

值得注意的是,在这两个通知的代码中有已经被遗弃的方法,在eclipse中会被黑线划掉。其相关文档是:

从中我们知道上面的方法和构造器在Android API 11中已被遗弃,下面我们来看看它推荐的方法:

上面实例的MainActivity中有这种推荐方法实现的代码(需要取消注释),在使用新的推荐代码时,请注释以下代码:

 Notification notify1 = new
Notification(R.drawable.ic_launcher, "又是一条短信",
System.currentTimeMillis()); notify1.setLatestEventInfo(MainActivity.this, "通知2", "点击查看详情",
pendingIntent);

运行程序就OK了,这时运行的就是推荐的方法了。

本文结束后又看到了一篇觉得不错的文章:http://blog.csdn.net/loongggdroid/article/details/17616509

状态栏通知Notification的简单使用的更多相关文章

  1. Android 状态栏通知Notification、NotificationManager简介

    Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...

  2. 【Android】状态栏通知Notification、NotificationManager详解(转)

    在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类:  NotificationMa ...

  3. Android的状态栏通知(Notification)

    通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...

  4. Android 状态栏通知 Notification

    private NotificationManager manager; private Notification.Builder builder; @Override protected void ...

  5. Android 状态栏通知Notification、NotificationManager详解

    http://www.cnblogs.com/onlyinweb/archive/2012/09/03/2668381.html

  6. 安卓状态栏通知Status Bar Notification

    安卓系统通知用户三种方式: 1.Toast Notification 2.Dialog Notification 3.Status Bar Notification Status Bar Notifi ...

  7. Android学习总结(十五) ———— Notification(状态栏通知)基本用法

    一.Notification基本概念  Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...

  8. 【WPF】右下角弹出自定义通知样式(Notification)——简单教程

    原文:[WPF]右下角弹出自定义通知样式(Notification)--简单教程 1.先看效果 2.实现 1.主界面是MainWindow 上面就只摆放一个Button即可.在Button的点击事件中 ...

  9. 通知 Notification 详解

    效果 通知栏-刚收到通知时 通知栏-收到通知几秒后 标准视图 大视图-下滑前是标准视图 大视图-下滑后显示大视图 自定义通知 讲解 Notification,俗称通知,是一种具有全局效果的通知,它展示 ...

随机推荐

  1. 面试准备--struts2.x

    对象解析: 1.HttpServletRequest对象是tomcat对用户请求信息的封装,该对象提供了多个方法可以获取用户的请求信息. 2.ActionContextCleanUP是一个可选过滤器, ...

  2. GPIO

    一.什么是GPIO?       首先应该理解什么是GPIO.GPIO,英文全称为General-Purpose IO ports,也就是通用IO口.在嵌入式系统中常常有数量众多,但是结构却比较简单的 ...

  3. TreeMap源码解析

    1.TreeMap介绍 TreeMap是一个通过红黑树实现有序的key-value集合. TreeMap继承AbstractMap,也即实现了Map,它是一个Map集合 TreeMap实现了Navig ...

  4. Nginx实现七层负载均衡配置指导

    本文描述了如何使用Nginx实现在应用层实现7层负载均衡功能,Nginx支持虚拟主机,可以按照轮询,IP哈希,URL哈希,权重方式对后端服务器做负载均衡,还支持后端服务器健康检查功能.废话不多说,详细 ...

  5. Android之parseSDKContent failed

    由于之前安装ADT之后就一直报parseSDKContent failed的错误,具体的信息为:java.lang.NullPointerException. 此问题的情况为在Eclipse下,And ...

  6. [codevs3862]竞赛班的垃圾处理

    题目描述 Description 竞赛班的师弟们都很懒,每个人都不喜欢倒垃圾,结果垃圾就会跟师兄的RP一样越堆越多,当然,老师时不时就会叫他们去倒垃圾.由于他们真的很懒,他们只会直接将垃圾桶最上面袋拿 ...

  7. leecode 归并排序 链表(java)

    写了好久,终于写成了.第一次zai leecode错题,题目质量很高,适合面试,与 1.归并排序是稳定的,在java中 Arrays.sort(a);中对于对象的排序就是归并排序.对于原子类型数据使用 ...

  8. ehcache在windows下使用RMI同步时出现的问题

    问题 最近的项目由于OP分配的机器没有配Redis或Mem,为了解决tomcat的集群问题,项目使用了Ehcache作为缓存.Ehcache使用RMI进行同步,但在本地测试时出现了缓存未同步的问题.最 ...

  9. map的基本操作函数及含义

    map的基本操作函数:      C++ Maps是一种关联式容器,包含“关键字/值”对      begin()          返回指向map头部的迭代器      clear()        ...

  10. XML与XHTML

    什么是XML XML的基本格式 XML的定义文档 HTML5的文档定义 XHTML1.0的文档定义 XHTML1.0标记格式 12.1 什么是XML XML中文翻译为可扩展标记语言,顾名思义,它比HT ...