Notification 使用详解(很全)

上传人:飞*** 文档编号:44401941 上传时间:2018-06-09 格式:DOC 页数:9 大小:54KB
返回 下载 相关 举报
Notification 使用详解(很全)_第1页
第1页 / 共9页
Notification 使用详解(很全)_第2页
第2页 / 共9页
Notification 使用详解(很全)_第3页
第3页 / 共9页
Notification 使用详解(很全)_第4页
第4页 / 共9页
Notification 使用详解(很全)_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《Notification 使用详解(很全)》由会员分享,可在线阅读,更多相关《Notification 使用详解(很全)(9页珍藏版)》请在金锄头文库上搜索。

1、Notification 使用详解(很全)Java 2:定义一个 Notification:Notification m_Notification=new Notification(); 3:设置 Notification 的各种属性: /设置通知在状态栏显示的图标m_Notification.icon=R.drawable.icon;/当我们点击通知时显示的内容 m_Notification.tickerText=“Button1 通知内容.“;通知时发出的默认声音m_Notification.defaults=Notification.DEFAULT_SOUND; /设置通知显示的参数In

2、tent m_Intent=new Intent(NotificationDemo.this,DesActivity.class); PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0); m_Notification.setLatestEventInfo(NotificationDemo.this, “Button1“, “Button1 通知“,m_PendingIntent ); /这个可以理解为开始执行这个通知m_NotificationManager

3、.notify(0,m_Notification); 4:既然可以增加同样我们也可以删除。当然是只是删除你自己增加的。m_NotificationManager.cancel(0); 这里的 0 是一个 ID 号码,和 notify 第一个参数 0 一样。 这也就完成了,添加删除工作。 这里我们还是一个 Demo 来掩饰我们的操作。 1:新建一个工程 NotificationDemo。 2:添加一个 Activity:DesActivity,注意需要在 Manifest.xml 中声明 3:NotificationDemo 中的 Laytout 文件很简单就是定义一个 Button.其代码文件

4、如下:1.package com.rocky.studio.ch4221;2.import android.app.Activity;3.import android.app.Notification;4.import android.app.NotificationManager;5.import android.app.PendingIntent;6.import android.content.Intent;7.import android.os.Bundle;8.import android.view.View;9.import android.widget.Button;10. im

5、port android.widget.TextView;11. public class NotificationDemo extends Activity 12.13. Button m_Button1;14. TextView m_txtView;15.16. NotificationManager m_NotificationManager;17. Notification m_Notification;18.19. Intent m_Intent;20. PendingIntent m_PendingIntent;21. 22. /* Called when the activity

6、 is first created. */23. Override24. public void onCreate(Bundle savedInstanceState) 25. super.onCreate(savedInstanceState);26. setContentView(R.layout.main);27. 28. m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);29. 30. 31. m_Button1=(Button)this.findViewById

7、(R.id.Button01);32. 33. 34. /点击通知时转移内容35. m_Intent=new Intent(NotificationDemo.this,DesActivity.class);36. 37. m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);38. 39. m_Notification=new Notification();40. 41. m_Button1.setOnClickListener(new Button.OnClickListener()4

8、2. public void onClick(View v) 43. / TODO Auto-generated method stub44. 45. /设置通知在状态栏显示的图标46. m_Notification.icon=R.drawable.icon;47. 48. /当我们点击通知时显示的内容49. m_Notification.tickerText=“Button1 通知内容.“;50. 51. /通知时发出的默认声音52. m_Notification.defaults=Notification.DEFAULT_SOUND;53. 54. /设置通知显示的参数55. m_Noti

9、fication.setLatestEventInfo(NotificationDemo.this, “Button1“, “Button1 通知“,m_PendingIntent );56. 57. /这个可以理解为开始执行这个通知58. m_NotificationManager.notify(0,m_Notification);59. 60. );61. 62. 63. 64. 65. 复制代码4:修改 DesActivity 的源文件,代码如下:它做的事情就是取消之前添加的 Notification1.package com.rocky.studio.ch4221;2.import a

10、ndroid.app.Activity;3.import android.app.NotificationManager;4.import android.os.Bundle;5.public class DesActivity extends Activity 6.7.NotificationManager m_NotificationManager;8.9.Override10. protected void onCreate(Bundle savedInstanceState) 11. / TODO Auto-generated method stub12. super.onCreate

11、(savedInstanceState); 13. this.setContentView(R.layout.main2);14. 15. /启动后删除之前我们定义的16. m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);17. m_NotificationManager.cancel(0); 18. 19.20. 复制代码代码也很简单。可以查看 Notification , NotificationMananger 这两个类来学习前后左右。下面是一篇文章,对 Noti

12、fication ,NotificationManager 这两个类有详细的说明介绍,特借鉴一下。NoticificationManager 很容易可以放在状态栏,也很容易实现从 statusbar 进入程序 中, NoticificationManager 中通过 intent 执行此程序的 activity 就可以了 NoticificationManager 状态栏操作 NotificationManager(通知管理器): NotificationManager 负责通知用户事件的发生. NotificationManager 有三个公共方法: 1. cancel(int id) 取消

13、以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走. 2. cancelAll() 取消以前显示的所有通知. 3. notify(int id, Notification notification) 把通知持久的发送到状态条上. /初始化 NotificationManager: NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification 代表着一个通知. Notification 的属性: audioStreamT

14、ype 当声音响起时,所用的音频流的类型 contentIntent 当通知条目被点击,就执行这个被设置的 Intent. contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示. defaults 指定哪个值要被设置成默认的. deleteIntent 当用户点击“Clear All Notifications“按钮区删除所有的通知的时候,这个被设置的 Intent 被执行. icon 状态条所用的图片. iconLevel 假如状态条的图片有几个级别,就设置这里. ledARGB LED 灯的颜色. ledOffMS LED 关闭时的闪光时间(以毫秒计算) led

15、OnMS 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 t

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

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