595人阅读 评论(0) 收藏 举报 Notification是Android中常用的一种通知方式,当有未读短信或者未接电话的时候,屏幕的状态栏就会有提示图标,这时可以下拉状态栏来 读取通知。在使用微信的时候(微信在后台运行),如果有新消息时便会发出声音提示,状态栏也有相应的微信提示。

Android中Notification通知的实现步骤:

1.获取NotificationManager对象
NotificationManager的三个公共方法:
①cancel(int id) 取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走。
②cancelAll() 取消以前显示的所有通知。
③notify(int id, Notification notification) 把通知持久的发送到状态条上。

2.初始化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的flags属性设置为FLAG_ONGOING_EVENT

view plain copy to clipboard print ?
  1. n.flags=Notification.FLAG_ONGOING_EVENT
n.flags = Notification.FLAG_ONGOING_EVENT

3.设置通知的显示参数
使用PendingIntent来包装通知Intent,使用Notification的setLatestEventInfo来设置通知的标题、通知内容等信息。

4.发送通知
使用NotificationManager的notify(int id, Notification notification)方法来发送通知。

下面代码演示下通知Notification的使用。完整代码下载:android_notification.rar

Notification实现的步骤:

view plain copy to clipboard print ?
  1. /**
  2. *MainActivity
  3. *@authorzuolongsnail
  4. */
  5. publicclassMainActivityextendsActivity{
  6. privateButtonnotifyBtn;
  7. privateButtoncancelBtn;
  8. privateNotificationManagernm;
  9. privateNotificationn;
  10. //通知的ID
  11. publicstaticfinalintID=1;
  12. @Override
  13. publicvoidonCreate(BundlesavedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. notifyBtn=(Button)findViewById(R.id.notify);
  17. cancelBtn=(Button)findViewById(R.id.cancel);
  18. notifyBtn.setOnClickListener(newMyOnClickListener());
  19. cancelBtn.setOnClickListener(newMyOnClickListener());
  20. //1.获取NotificationManager对象
  21. nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  22. //2.初始化Notification对象
  23. n=newNotification();
  24. //设置通知的icon
  25. n.icon=R.drawable.notify;
  26. //设置通知在状态栏上显示的滚动信息
  27. n.tickerText="一个通知";
  28. //设置通知的时间
  29. n.when=System.currentTimeMillis();
  30. }
  31. classMyOnClickListenerimplementsOnClickListener{
  32. @Override
  33. publicvoidonClick(Viewv){
  34. switch(v.getId()){
  35. caseR.id.notify:
  36. //3.设置通知的显示参数
  37. Intentintent=newIntent(MainActivity.this,NotificationView.class);
  38. PendingIntentpi=PendingIntent.getActivity(MainActivity.this,0,intent,0);
  39. n.setLatestEventInfo(MainActivity.this,"通知标题","通知内容",pi);
  40. //4.发送通知
  41. nm.notify(ID,n);
  42. break;
  43. caseR.id.cancel:
  44. //取消通知
  45. nm.cancel(ID);
  46. break;
  47. }
  48. }
  49. }
  50. }

打开通知后跳转到的Activity:

view plain copy to clipboard print ?
  1. /**
  2. *打开通知后跳转的Activity
  3. *@authorzuolongsnail
  4. */
  5. publicclassNotificationViewextendsActivity{
  6. @Override
  7. protectedvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.view);
  10. //取消通知
  11. NotificationManagernm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  12. nm.cancel(MainActivity.ID);
  13. }
  14. }

截图:

补充:

Notification提示方式:
①在状态栏(Status Bar)显示的通知文本提示,如:
notification.tickerText = "Hello Notification";

②发出提示音,如:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.sound = Uri.parse("
file:///sdcard/notification/ringer.mp3");
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

③手机振动,如:
notification.defaults |= Notification.DEFAULT_VIBRATE;

// 100 毫秒延迟后,震动 200 毫秒,暂停 100 毫秒后,再震动 300 毫秒
long[] vibrate = {100,200,100,300};
notification.vibrate = vibrate;

注:需要加权限<uses-permission android:name="android.permission.VIBRATE"/>

④LED灯闪烁,如:
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

更多相关文章

  1. Validates using resource IDs in a switch statement in Androi
  2. Android(安卓)CheckBox
  3. Android(安卓)Studio安装apk失败
  4. Android:蓝牙耳机断开连接,音频播放器暂停播放
  5. 【Android翻译】关于Activity的onSaveInstanceState调用时机的说
  6. Android样式:selector(选择器)
  7. Android(安卓)监听开机广播
  8. phonegap推送实现(android)
  9. android 监听网络状态的变化及实战

随机推荐

  1. android的抽屉控件SlidingDrawer的使用
  2. 八款开源Android游戏引擎
  3. Android之NDK开发
  4. Android中操作SQLite数据库
  5. Android安装器(mac版) V1.0.0
  6. Android(安卓)数字签名学习笔记
  7. [置顶] 如何使Android应用程序获取系统权
  8. 关于Android的Holo主题
  9. Android中显式和隐式intent的特点和区别
  10. Android权威官方屏幕适配全攻略