展会信息港展会大全

android Notification的属性操作方法 创建 更新 自定义通知视图
来源:互联网   发布日期:2015-10-02 21:43:58   浏览:1486次  

导读:最近在使用Notification,要使用到LED的控制,所以在网上找了些资料:audioStreamType 当声音响起时,所用的音频流的类型contentIntent 当通知条目被点击,就执行这个被设置的Intent.contentView 当通知被显示......

最近在使用Notification,要使用到LED的控制,所以在网上找了些资料:

audioStreamType 当声音响起时,所用的音频流的类型

contentIntent 当通知条目被点击,就执行这个被设置的Intent.

contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示.

defaults 指定哪个值要被设置成默认的.

deleteIntent 当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行.

icon 状态条所用的图片.

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

ledARGB LED灯的颜色.

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

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

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

sound 通知的声音

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

vibrate 振动模式.

when 通知的时间戳.

其基本操作方法:

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,然后重新发送一次通知即可。

3. 自定义通知视图

这部分可以参考官方文档,讲的很详细了。

AndroidSDK: docs/guide/topics/ui/notifiers/notifications.html

赞助本站

人工智能实验室

相关热词: Notification 属性 通知

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港