




已阅读5页,还剩13页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
android Notification 的使用最近一直在研究 android ,并一边研究一边做应用。其中遇到了把程序通知常驻在 Notification 栏,并且不能被 clear 掉(就像android QQ一样)的问题。经过研究实现了其功能,现把 Notification 的使用总结如下: Notification 的使用需要导入 3 个类?123import android.app.PendingIntent;import android.app.NotificationManager;import android.app.Notification;代码示例及说明?123456789101112131415161718NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Notification n = new Notification(R.drawable.chat, Hello,there!, System.currentTimeMillis(); n.flags = Notification.FLAG_AUTO_CANCEL; Intent i = new Intent(arg0.getContext(), NotificationShow.class);i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); /PendingIntentPendingIntent contentIntent = PendingIntent.getActivity(arg0.getContext(), R.string.app_name, i, PendingIntent.FLAG_UPDATE_CURRENT);n.setLatestEventInfo(arg0.getContext(),Hello,there!, Hello,there,Im john., contentIntent);nm.notify(R.string.app_name, n);下面依次对每一段代码进行分析:?1NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);创建 NotificationManager,其中创建的 nm 对象负责“发出”与“取消” Notification。?12Notification n = new Notification(R.drawable.chat, Hello,there!, System.currentTimeMillis(); n.flags = Notification.FLAG_ONGOING_EVENT; 创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。其中创建的 n 对象用来描述出现在系统通知栏的信息,之后我们将会看到会在 n 对象上设置点击此条通知发出的Intent。?1n.flags = Notification.FLAG_AUTO_CANCEL;设置 n.flags 为 Notification.FLAG_AUTO_CANCEL ,该标志表示当用户点击 Clear 之后,能够清除该通知。?12Intent i = new Intent(arg0.getContext(), NotificationShow.class);i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity。更多请参见 “ (转载)Android下Affinities和Task ”?123456/PendingIntentPendingIntent contentIntent = PendingIntent.getActivity(arg0.getContext(), R.string.app_name, i, PendingIntent.FLAG_UPDATE_CURRENT);PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位。其中 PendingIntent.FLAG_UPDATE_CURRENT 表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据。这里再简要说一下 Intent 与 PendingIntent 的区别:Intent :意图,即告诉系统我要干什么,然后系统根据这个Intent做对应的事。如startActivity相当于发送消息,而Intent是消息的内容。PendingIntent :包装Intent,Intent 是我们直接使用 startActivity , startService 或 sendBroadcast 启动某项工作的意图。而某些时候,我们并不能直接调用startActivity , startServide 或 sendBroadcast ,而是当程序或系统达到某一条件才发送Intent。如这里的Notification,当用户点击Notification之后,由系统发出一条Activity 的 Intent 。因此如果我们不用某种方法来告诉系统的话,系统是不知道是使用 startActivity ,startService 还是 sendBroadcast 来启动Intent 的(当然还有其他的“描述”),因此这里便需要PendingIntent。?12345n.setLatestEventInfo(arg0.getContext(),Hello,there!, Hello,there,Im john., contentIntent);设置显示在通知下拉框中的信息,参数依次为:Context,标题,内容,PendingIntent。?1nm.notify(R.string.app_name, n);启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification。如何使自己的Notification像Android QQ一样能出现在 “正在运行的”栏目下面其实很简单,只需设置Notification.flags = Notification.FLAG_ONGOING_EVENT;便可以了。如何改变 Notification 在“正在运行的”栏目下面的布局创建 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 便可以了,如:?12345678910111213PendingIntent contentIntent = PendingIntent.getActivity(arg0.getContext(), R.string.app_name,i, PendingIntent.FLAG_UPDATE_CURRENT);RemoteViews rv = new RemoteViews(Main.this.getPackageName(), R.layout.notification_view);rv.setImageViewResource(R.id.image, R.drawable.chat);rv.setTextViewText(R.id.text, Hello,there,Im john.);n.contentView = rv;n.contentIntent = contentIntent;nm.notify(R.string.app_name, n);注意,如果使用了contentView,那么便不要使用Notification.setLatestEventInfo。如果setLatestEventInfo在赋给 Notification.contentView 的代码之后,那么contentView的效果将被覆盖,显示的便是 setLatestEventInfo 的效果;如果 setLatestEventInfo 在 Notification.contentView 的代码之前,那么显示的便是 Notification.contentView 的效果,也就是说不管你想要setLatestEventInfo 或 contentView 的自定义效果,请保证始终只有一句设置代码,因为在最后一句绑定的时候,之前的设置contentView或setLatestEventInfo的代码都是完全没有必要的。android之Notification通知 分类: 杂乱知识速记2011-03-14 21:407679人阅读评论(12)收藏举报androidbuttonnotificationsservice手机manager我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的。java:nogutter view plaincopyprint?1. package .chenzheng_java; 2.3. import android.app.Activity; 4. import android.app.Notification; 5. import android.app.NotificationManager; 6. import android.app.PendingIntent; 7. import android.content.Context; 8. import android.content.Intent; 9. import .Uri; 10. import android.os.Bundle; 11. import vider.MediaStore.Audio; 12. import android.view.View; 13. import android.widget.Button; 14.15. /* 16. * description 状态栏通知相关 17. * author chenzheng_java 18. * 19. */ 20. public class NotificationActivity extends Activity 21. Override 22. protected void onCreate(Bundle savedInstanceState) 23. super.onCreate(savedInstanceState); 24. setContentView(R.layout.notification); 25.26. Button button = (Button) findViewById(R.id.button); 27. button.setOnClickListener(new View.OnClickListener() 28.29. Override 30. public void onClick(View v) 31. addNotificaction(); 32.33. 34. ); 35.36. 0.41. /* 42. * 添加一个notification 43. */ 44. private void addNotificaction() 45. NotificationManager manager = (NotificationManager) this 46. .getSystemService(Context.NOTIFICATION_SERVICE); 47. / 创建一个Notification 48. Notification notification = new Notification(); 49. / 设置显示在手机最上边的状态栏的图标 50. notification.icon = R.drawable.excel; 51. / 当当前的notification被放到状态栏上的时候,提示内容 52. notification.tickerText = 注意了,我被扔到状态栏了; 53.54. /* 55. * notification.contentIntent:一个PendingIntent对象,当用户点击了状态栏上的图标时,该Intent会被触发 56. * notification.contentView:我们可以不在状态栏放图标而是放一个view 57. * notification.deleteIntent 当当前notification被移除时执行的intent 58. * notification.vibrate 当手机震动时,震动周期设置 59. */ 60. / 添加声音提示 61. notification.defaults=Notification.DEFAULT_SOUND; 62. / audioStreamType的值必须AudioManager中的值,代表着响铃的模式 63. notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER; 64.65. /下边的两个方式可以添加音乐 66. /notification.sound = Uri.parse(file:/sdcard/notification/ringer.mp3); 67. /notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, 6); 68. Intent intent = new Intent(this, Notification2Activity.class); 69. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 70. / 点击状态栏的图标出现的提示信息设置 71. notification.setLatestEventInfo(this, 内容提示:, 我就是一个测试文件, pendingIntent); 72. manager.notify(1, notification); 73.74. 75.76. 点击按钮时候,状态栏会显示:看到了吧,状态栏多出来一个excel图标,当我按住图标不放,往下拖动的时候,出来了这个页面然后,当我们点击这个对话框之后,就会触发intent,跳转到Notification2Activity.java这个activity。-注意,NotificationManager里的notify(id,notification)中的id是用来唯一标识我们当前的这个notification的标识符,我们通过cancel方法删除通知时,传递的就是这个值。可能读者在看很多文档的时候,发现这个地方指定了一个莫名奇妙的值,例如R.drawable.icon,很多朋友就纳闷了,为什么这里要指定一个图片呢。这里笔者就介绍下,为什么呢?答案其实很简单,我们都知道,我们这里对参数的唯一要求就是,这个id要和notify方法中的一致,并且是唯一;只要满足了这两项,其他的都无所谓。notify和cancel里一致我们作为开发者,太好控制了,但是唯一呢,我们还真不好说,于是这里就有些人动小脑筋了,很巧妙的用了我们系统中的图片资源或者其他资源的索引ID,我们都知道,这些值肯定都是唯一的!-下面是从网上找的一些资料:如果要添加一个Notification,可以按照以下几个步骤1:获取NotificationManager:NotificationManager m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);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;/设置通知显示的参数Intent 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.notify(0,m_Notification);4:既然可以增加同样我们也可以删除。当然是只是删除你自己增加的。m_NotificationManager.cancel(0); 这里的0是一个ID号码,和notify第一个参数0一样。这也就完成了,添加删除工作。-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提供了丰富的手机提示方式: 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); Android Notification介绍2011-12-01 21:02:22 我来说两句 收藏 我要投稿Notification就是在桌面的状态通知栏。这主要涉及三个主要类:Notification:设置通知的各个属性。NotificationManager:负责发送通知和取消通知Notification.Builder:Notification内之类,创建Notification对象。非常方便的控制所有的flags,同时构建Notification的风格。主要作用:1.创建一个状态条图标。2.在扩展的状态条窗口中显示额外的信息(和启动一个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 notify(int id, Notification notification),notify(String tag, int id, Notification notification)看看这五个函数就知道这个类的作用了。但是在初始化对象的时候要注意:NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);二、Notification设置这个类主要是设置Notification的相关属性。初始化Notification n = new Notification();Notification里面有很多属性下面选择几个常用的介绍一下icon 这个是设置通知的图标。像QQ的小企鹅sound 这个是设置来通知时的提示音。tickerText 设置提示的文字。vibrate 来通知时振动。when 设置来通知时的时间flag 这个很有意思是设置通知在状态栏显示的方式。它的值可以设置为虾米这些值:FLAG_NO_CLEAR 将flag设置为这个属性那么通知栏的那个清楚按钮就不会出现FLAG_ONGOING_EVENT 将flag设置为这个属性那么通知就会像QQ一样一直在状态栏显示DEFAULT_ALL 将所有属性设置为默认DEFAULT_SOUND 将提示声音设置为默认DEFAULT_VIBRATE 将震动设置为默认三、Notification.Builder这个类一般用于管理Notification,动态的设置Notification的一些属性。即用set来设置。也没啥好说的。看一个例子:这个例子还需要在xml中添加两个按钮public class Main ext
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 晨光文具店营销方案策划
- 提供建筑方案设计流程
- 学校师德师风建设工作五年规划
- 建筑工程施工现场消防安全方案
- 员工培训管理实施细则
- 建筑方案设计前期分析论文
- 营销推广方案服装店文案
- 2025年注册会计师(CPA)考试 企业并购重组科目冲刺押题试卷及重点解读
- 精密机械行业分析报告
- 《函数的概念与性质》九年级数学代数教学方案
- USCAR培训资料完整版经典培训教材课件
- 涂漆检验报告(面漆)
- 制药工程专业导论03.中药制药课件
- 肿瘤生物免疫治疗及护理-课件
- 小学数学四年级上册《数对》课件
- 高中英语选择性必修一 Unit 2 Assessing your progress(34张)
- 液压传动全套ppt课件(完整版)
- 《基础统计》教学案例“郑州市大瓶装纯水市场调查”统计应用案例
- 建设工程施工合同(示范文本)解读课件
- 南瑞继保后台监控使用厂家培训版本
- 高中美术 《设计》艺术与技术的结合——产品设计 1 课件
评论
0/150
提交评论