android notification详细解读

上传人:第*** 文档编号:31309810 上传时间:2018-02-06 格式:DOC 页数:7 大小:32.50KB
返回 下载 相关 举报
android notification详细解读_第1页
第1页 / 共7页
android notification详细解读_第2页
第2页 / 共7页
android notification详细解读_第3页
第3页 / 共7页
android notification详细解读_第4页
第4页 / 共7页
android notification详细解读_第5页
第5页 / 共7页
点击查看更多>>
资源描述

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

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

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

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

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

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

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

7、fy(int id, Notification notification),notify(String tag, int id, Notification notification)看看这五个函数就知道这个类的作用了。但是在初始化对象的时候要注意:NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);二、Notification设置这个类主要是设置Notification的相关属性。初始化Notification n = new Notification();Notificat

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

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

10、;Overridepublic void onCreate(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 =

11、(NotificationManager)this.getSystemService(service);/实例化 Notificationn = 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.fla

12、gs = Notification.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) /实例化IntentIntent

13、intent = new Intent(Main.this, Main.class);/获取PendingIntentPendingIntent 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()Overridepublic vo

14、id onClick(View v) nm.cancel(ID);android notification 事件nm = (NotificationMJava代码 1. nm = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);2. Notification notification = new Notification(android.R.drawable.stat_sys_download, itemname, System 3. .currentTimeMillis(); 4. In

15、tent notificationIntent = new Intent(context, InstallActivity. class); 5. notificationIntent.putExtra(hasdownloaded , false ); 6. notificationIntent.putExtra(oranotificationid , oranotificationid); 7. PendingIntent contentIntent = PendingIntent.getActivity(context, 8. this .oranotificationid, 9. notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 10. / notification.contentIntent = contentIntent; 11. 12. notification.setLatestEventInfo(context, null , null , 13. contentIntent); 14. notification.contentView = new RemoteView

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

当前位置:首页 > 中学教育 > 其它中学文档

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