`
mmdev
  • 浏览: 12920373 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Notification 使用详解

 
阅读更多

本文转载与 Eoeandroid论坛

下面是一篇文章,对Notification ,NotificationManager这两个类有详细的说明介绍,特借鉴一下。
NoticificationManager很容易可以放在状态栏,也很容易实现从statusbar进入程序 中,
NoticificationManager中通过intent执行此程序的activity就可以了

NoticificationManager状态栏操作

NotificationManager(通知管理器):
NotificationManager负责通知用户事件的发生.
NotificationManager有三个公共方法:
1. cancel(int id) 取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走.

2. cancelAll() 取消以前显示的所有通知.
3. notify(int id,Notification notification) 把通知持久的发送到状态条上.


//初始化NotificationManager:
NotificationManager nm =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification代表着一个通知.
Notification的属性:
audioStreamType 当声音响起时,所用的音频流的类型
contentIntent 当通知条目被点击,就执行这个被设置的Intent.
contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示.
defaults 指定哪个值要被设置成默认的.
deleteIntent 当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行.
icon 状态条所用的图片.

iconLevel 假如状态条的图片有几个级别,就设置这里.

ledARGB LED 灯的颜色.

ledOffMS LED 关闭时的闪光时间(以毫秒计算)

ledOnMS LED 开始时的闪光时间(以毫秒计算)

number 这个通知代表事件的号码

sound 通知的声音
tickerText 通知被显示在状态条时,所显示的信息

vibrate 振动模式.

when 通知的时间戳.

将Notification发送到状态条上:
Notification notification = new Notification();
Notification的设置过程……..
nm.notify(0, notification); //发送到状态条上

创建和触发Notification

创建和配置新的Notification需要经历三步。

首先你要创建一个新的Notification对象,传入要在状态条上显示的图标文字Notification的当前时间,如下面的代码片段所示:

// Choose a drawable to display as the status bar icon
int icon = R.drawable.icon;

// Text to display in the status bar when the notification is launched
String tickerText = “Notification”;

// The extended status bar orders notification in time order
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);

当Notification触发时,文本将沿着状态条进行滚动 显示。

其次,使用setLatestEventInfo方法来配置Notification在扩展的状态窗口中的外观。扩展的状态窗口将显示图标和在构造函数中传入的时间,以及显示标题和一个详细的字符串。Notification一般表示为对一个动作的请求或引起用户的注意,所以,当用户点击Notification项目时你可以指定一个PendingIntent来触发。

下面的代码片段使用了setLastestEventInfo来设置这些值:

Context context = getApplicationContext();

// Text to display in the extended status window
String expandedText = “Extended status text”;

// Title for the expanded status
String expandedTitle = “Notification Title”;

// Intent to launch an activity when the extended text is clicked
Intent intent = new Intent(this, MyActivity.class);
PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context,
expandedTitle,
expandedText,
launchIntent);

一个好的形式是显示相同事件(例如,接收多个SMS消息)的多个对象时 使用一个Notification图 标。为了呈现给用户,使用setLastestEventInfo更新数据集来呈现最近的消息以及重新触发Notification来更新它的值。

你还可以使用number属性来显示一个状态条图标所表示的事件数目。

设置为比1大的数,如下所示,将在状态条图标上以一个小小的数字进行 显示:

notification.number++;

任何对Notification的变更,你都需要重新触发来进行更新。如果要删除这个数字,设置number的值为0或者-1。

最后,你可以对Notification对象使用多种属性来增强Notification的效果,如闪烁LED、震动电话和播放音乐文件。这些高级特征将在本章的后面部分进行详细描述。

触发Notification

为了触发一个Notification,使用NotificationManager的notify方法,将一个整数的ID和Notification对象传入,如下的片段所示:

int notificationRef = 1;
notificationManager.notify(notificationRef, notification);

为了更新一个已经触发过的Notification,传入相同的ID。你既可以传入相同的Notification对象,也可以是一个全新的对象。只 要ID相同,新的Notification对象会替换状态条
图标和扩展的状态窗口的细节。

你还可以使用ID来取消Notification,通过调用NotificationManager的cancel方法,如下所示:

notificationManager.cancel(notificationRef);

取消一个Notification时,将移除它的状态条图标以及清除 在扩展的状态窗口中的信息。


Notification和NotificationManager的基本使用方法

1. NotificationManager和Notification用来设置通知。

通知的设置等操作相对比较简单,基本的使用方式就是用新建一个Notification对象,然后设置好通知的各项参数,然后使用系统后台运行的 NotificationManager服务将通知发出来。

基本步骤如下:

1)得到NotificationManager:

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);



2)创建一个新的Notification对象:

Notification notification = new Notification();

notification.icon = R.drawable.notification_icon;



也可以使用稍微复杂一些的方式创建Notification:

int icon = R.drawable.notification_icon; //通知图标

CharSequence tickerText = "Hello";//状态栏(Status Bar)显示的通知文本提示

long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

Notification notification = new Notification(icon, tickerText, when);



3)填充Notification的各个属性:

Context context = getApplicationContext();

CharSequence contentTitle = "My notification";

CharSequence contentText = "Hello World!";

Intent notificationIntent = new Intent(this, MyClass.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);



Notification提供了丰富的手机提示方式:

a)在状态栏(Status Bar)显示的通知文本提示,如:

notification.tickerText = "hello";



b)发出提示音,如:

notification.defaults |= Notification.DEFAULT_SOUND;

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");



c)手机振动,如:

notification.defaults |= Notification.DEFAULT_VIBRATE;

long[] vibrate = {0,100,200,300};

notification.vibrate = vibrate;



d)LED灯闪烁,如:

notification.defaults |= Notification.DEFAULT_LIGHTS;

notification.ledARGB = 0xff00ff00;

notification.ledOnMS = 300;

notification.ledOffMS = 1000;

notification.flags |= Notification.FLAG_SHOW_LIGHTS;


4)发送通知:

private static final int ID_NOTIFICATION = 1;

mNotificationManager.notify(ID_NOTIFICATION, notification);


2. 通知的更新

如果需要更新一个通知,只需要在设置好notification之后,再调用setLatestEventInfo,然后重新发送一次通知即可。

分享到:
评论

相关推荐

    Notification 使用详解(很全)

    Notification 使用详解(很全)Notification 使用详解(很全)

    Notification使用详解之一:基础应用

    在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态...

    Notification用法详解,包含自定义视图

    Notification用法详解,包含多种用法,

    Android Notification使用方法详解

    Android Notification使用详解 Notification 核心代码(链式调用):适用于Android 4.0以上(不兼容低版本) Notification noti = new Notification.Builder(this) .setContentTitle(标题名称) .setContentText...

    Notification案例详解

    关于Notification案例的详解,希望对你有所帮助吧

    Android Notification 使用方法详解

    主要介绍了Android Notification 使用方法详解的相关资料,这里提供实例来帮助大家理解掌握这部分内容,需要的朋友可以参考下

    android notification完全解析Demo

    android notification完全解析Demo

    android 通知Notification详解及实例代码

    主要介绍了android 通知Notification详解及实例代码的相关资料,需要的朋友可以参考下

    android Notification详解

    简单介绍Android Notification用法 详细介绍NotificationManageService和StatusBarManageService与App层交互过程

    notification详解

    notification类表示一个持久的通知,将提交给用户使用NotificationManager。已添加的Notification.Builder,使其更容易构建通知。

    Notification详解

    Notification安卓手机通知栏的常见到,该demo包含系统默认样式通知以及自定义样式的通知,能自定义要多炫自己就可以随心布局修改;具体请参考http://blog.csdn.net/daitu_liang/article/details/50246803

    Android 推送原理(Android Push Notification)详解

    主要介绍了Android 推送原理(Android Push Notification)详解的相关资料,这里对Android 推送的原理做了简单的介绍,需要的朋友可以参考下

    Android编程开发之NotiFication用法详解

    在帮助文档中,是这么说的, notification类表示一个持久的通知,将提交给用户使用NotificationManager。已添加的Notification.Builder,使其更容易构建通知。 notification是一种让你的应用程序在没有开启情况下或...

    详解Android中Notification的使用方法

    在消息通知的时候,我们经常用到两个控件Notification和Toast。特别是重要的和需要长时间显示的信息,用Notification最合适不过了。他可以在顶部显示一个图标以标示有了新的通知,当我们拉下通知栏的时候,可以看到...

    Android开发之Notification通知用法详解

    本文实例讲述了Android开发之Notification通知用法。分享给大家供大家参考,具体如下: 根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把...

Global site tag (gtag.js) - Google Analytics