ANDROIDNOTIFICATION详细解读

上传人:汽*** 文档编号:505319273 上传时间:2022-11-24 格式:DOC 页数:7 大小:49.50KB
返回 下载 相关 举报
ANDROIDNOTIFICATION详细解读_第1页
第1页 / 共7页
ANDROIDNOTIFICATION详细解读_第2页
第2页 / 共7页
ANDROIDNOTIFICATION详细解读_第3页
第3页 / 共7页
ANDROIDNOTIFICATION详细解读_第4页
第4页 / 共7页
ANDROIDNOTIFICATION详细解读_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《ANDROIDNOTIFICATION详细解读》由会员分享,可在线阅读,更多相关《ANDROIDNOTIFICATION详细解读(7页珍藏版)》请在金锄头文库上搜索。

1、Android Notification详细解读NotificationManager(通知管理器):NotificationManager负责通知用户事件的发生.NotificationManager有三个公共方法:1. cancel(int id) 取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走.2. cancelAll() 取消以前显示的所有通知.3. notify(int id, Notification notification) 把通知持久的发送到状态条上.Java代码/初始化NotificationManager:Notifica

2、tionManager nm =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);Java代码/初始化NotificationManager:NotificationManager nm =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);/初始化NotificationManager:NotificationManager nm =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);No

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

4、ledOffMS LED关闭时的闪光时间(以毫秒计算)ledOnMS LED开始时的闪光时间(以毫秒计算)number 这个通知代表事件的号码sound 通知的声音tickerText 通知被显示在状态条时,所显示的信息vibrate 振动模式.when 通知的时间戳.Notification的公共方法:describeContents() Describe the kinds of special objects contained in this Parcelables marshalled representation.setLatestEventInfo(Context context

5、, CharSequence contentTitle, CharSequenceAndroid Notification介绍Notification就是在桌面的状态通知栏。这主要涉及三个主要类:Notification:设置通知的各个属性。NotificationManager:负责发送通知和取消通知Notification.Builder:Notification内之类,创建Notification对象。非常方便的控制所有的flags,同时构建Notification的风格。主要作用:1.创建一个状态条图标。2.在扩展的状态条窗口中显示额外的信息(和启动一个Intent)。3.闪灯或LED

6、。4.电话震动。5.发出听得见的警告声(铃声,保存的声音文件)。Notification是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径下面主要介绍这三个类:一、NotificationManager这个类是这三个类中最简单的。主要负责将Notification在状态显示出来和取消。主要包括5个函数:void cancel(int id),void cancel(String tag, int id),void cancelAll(),void notify(int id, Notification no

7、tification),notify(String tag, int id, Notification notification)看看这五个函数就知道这个类的作用了。但是在初始化对象的时候要注意:NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);二、Notification设置这个类主要是设置Notification的相关属性。初始化Notification n = new Notification();Notification里面有很多属性下面选择几个常用的介绍一下ico

8、n 这个是设置通知的图标。像QQ的小企鹅sound 这个是设置来通知时的提示音。tickerText 设置提示的文字。vibrate 来通知时振动。when 设置来通知时的时间flag 这个很有意思是设置通知在状态栏显示的方式。它的值可以设置为虾米这些值:FLAG_NO_CLEAR 将flag设置为这个属性那么通知栏的那个清楚按钮就不会出现FLAG_ONGOING_EVENT 将flag设置为这个属性那么通知就会像QQ一样一直在状态栏显示DEFAULT_ALL 将所有属性设置为默认DEFAULT_SOUND 将提示声音设置为默认DEFAULT_VIBRATE 将震动设置为默认三、Notific

9、ation.Builder这个类一般用于管理Notification,动态的设置Notification的一些属性。即用set来设置。也没啥好说的。看一个例子:这个例子还需要在xml中添加两个按钮public class Main extends Activity private Button sendBtn , cancelBtn; private Notification n; private NotificationManager nm; /Notification的标示ID private static final int ID = 1; Override public void on

10、Create(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); /实例化按钮 sendBtn = (Button)this.findViewById(R.id.sendBtn); cancelBtn = (Button)this.findViewById(R.id.cancelBtn); /获取NotificationManager实例 String service = NOTIFICATION_SERVICE; nm = (NotificationMana

11、ger)this.getSystemService(service); /实例化Notification n = new Notification(); /设置显示图标,该图标会在状态栏显示 int icon = R.drawable.icon; /设置显示提示信息,该信息也会在状态栏显示 String tickerText = Test Notifaction; /显示时间 long when = System.currentTimeMillis(); n.icon = icon; n.tickerText = tickerText; n.when = when; n.flags = Not

12、ification.FLAG_NO_CLEAR; n.flags = Notification.FLAG_ONGOING_EVENT; /为按钮添加监听器 sendBtn.setOnClickListener(sendClickListener); cancelBtn.setOnClickListener(cancelClickListener); private OnClickListener sendClickListener = new OnClickListener() Override public void onClick(View v) /实例化Intent Intent int

13、ent = new Intent(Main.this, Main.class); /获取PendingIntent PendingIntent pi = PendingIntent.getActivity(Main.this, 0, intent, 0); /设置事件信息 n.setLatestEventInfo(Main.this, My Title, My Content, pi); /发出通知 nm.notify(ID, n); ;private OnClickListener cancelClickListener = new OnClickListener() Override public void onClick(View v) nm.cancel(ID); ;android notification 事件nm=(NotificationMJava代码 1. nm=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);2. Notificationnotification=newNotification(android.R.drawable.stat_sys_download,itemname,System3. .currentTimeMillis();4. Int

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号