Notification

状态栏提醒

    在Android 手机界面最上方有一条显示时间,信号强度和电池状态等信息的区域,这是 Android 手机的状态栏StatusBar。当系统有一些重要的信息要通知手机用户时,例如收到新短信,或者是收到新邮件,或者是有未接电话等等,系统通常会把信息显示在状态栏中,有的仅显示小图片,有的则显示文字和小图片,用手指按住状态栏往下拉,还可以展开状态栏,查看所有系统发出的信息。

    而在Android 中有提醒功能的也可以用 AlertDialog,但是我们要慎重的使用,因为当使用 AlertDialog的时候,用户正在进行的操作将会被打断,当前焦点被 AlertDialog得到。我们可以想像一下,当用户打游戏正爽的时候,这时候来了一条短信。如果这时候短信用AlertDialog提醒,用户必须先去处理这条提醒,从而才能继续游戏。用户可能会活活被气死。而使用Notification就不会带来这些麻烦事,用户完全可以打完游戏再去看这条短信。所以在开发中应根据实际需求,选择合适的控件。

    Notification可以在屏幕最顶部的状态栏上显示一个图标通知,通知的同时可以播放声音,以及振动来提示用户,点击通知还可以进入指定的 Activity。

 

NotificationManager

·获取实例:

  NotificationManager nm= (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

·公共方法:

Android <wbr>- <wbr>NotificationManager

Notification

Android <wbr>- <wbr>NotificationManager Android <wbr>- <wbr>NotificationManager Android <wbr>- <wbr>NotificationManager

状态栏提醒必须设置的三个方面

·提示图标

·提醒的标题和提醒的内容文本

·PendingIntent

状态栏提醒的可选设置

·提醒文本( ticker-text )

·声音提醒

·振动提醒

·灯光闪烁

 

使用步骤

1、获取NotificationManager:

   NotificationManagerm_NotificationManager = 

     (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

2、定义一个Notification: 

  Notification  m_Notification = newNotification();

3、设置 Notification 的各种属性:

   //设置通知在状态栏显示的图标
  m_Notification.icon = R.drawable.icon;

   //当我们点击通知时显示的内容
  m_Notification.tickerText = "Button1通知内容.....";     

   //通知时发出的默认声音
  m_Notification.defaults = Notification.DEFAULT_SOUND;

   //设置通知显示的参数

  Intent m_Intent = new Intent(this,DesActivity.class); 

   PendingIntent m_PIntent =PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent,0);

  m_Notification.setLatestEventInfo(this, "Button1", "Button1通知",m_PIntent );

   //这个可以理解为开始执行这个通知
  m_NotificationManager.notify(0, m_Notification);

4、既然可以增加同样我们也可以删除,当然是只是删除你自己增加的。

  m_NotificationManager.cancel(0); //这里的0是一个ID号码,和notify第一个参数0一样。

 

提醒的更新

每一个提醒都对应有一个唯一标识符ID,在发布提醒时指定的:

NotificationManager.notify(ID,notification)   

在更新提醒的时候,只需要在修改Notification的一些属性之后,再调用其setLatestEventInfo(),然后重新发送一次通知即可。

 

Notification丰富的提示方式

a) tickerText:显示文本提示

   notification.tickerText = "hello";

b) sound:发出提示音

    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) vibrate:手机振动

    notification.defaults |= Notification.DEFAULT_VIBRATE;

    long[] vibrate = {0,100,200,300};

    notification.vibrate = vibrate;

d) ledARGBledOnMSledOffMS:LED灯闪烁

    notification.defaults |= Notification.DEFAULT_LIGHTS;

    notification.ledARGB = 0xff00ff00;

    notification.ledOnMS = 300;

    notification.ledOffMS = 1000;

    notification.flags |= Notification.FLAG_SHOW_LIGHTS;

 

声音提醒

·使用默认声音

 notification.defaults|=Notification.DEFAULT_SOUND;

·使用自定义声音

 notification.sound=Uri.parse("file:///sdcard/notification/ringer.mp3");

·注:如果定义了默认声音,那么自定义声音将被覆盖

 

振动提醒

·使用默认振动

 notification.defaults|=Notification.DEFAULT_VIBRATE;

·使用自定义振动

  long[]vibrate ={0,100,200,300}; 
 notification.vibrate= vibrate;

·注:如果定义了默认振动,那么自定义振动将被覆盖

 

灯光闪烁提醒

·使用默认闪烁

 notification.defaults|=Notification.DEFAULT_LIGHTS;

·使用自定义闪烁

 notification.ledARGB=0xff00ff00; //LED灯的颜色,绿灯
 notification.ledOnMS=300; // LED灯显示的毫秒数,300毫秒
 notification.ledOffMS=1000; // LED灯关闭的毫秒数,1000毫秒
 notification.flags |=Notification.FLAG_SHOW_LIGHTS;// 必须加上这个标志

 

更多特性

可以通过 Notification 的相关字段或标志(flags)为提醒设置更多的特性。

·FLAG_AUTO_CANCEL标志:当提醒被用户点击之后会自动被取消(cancel);

·FLAG_INSISTENT标志:在用户响应之前会一直重复提醒音;

·FLAG_ONGOING_EVENT 标志:Add this to theflags field to group the notification under

  the "Ongoing" title inthe Notifications window.

·FLAG_NO_CLEAR标志:当在提醒栏中点击“清除提醒”按钮时,该提醒将不会被清除;

·number 字段:This value indicatesthe current number of events represented by thenotification.

  The appropriate numberis overlaid on top of the status bar icon. If you intend to usethis

  field, then you must start with "1"when the Notification is first created. (If you change

  the value from zero to anything greaterduring an update, the number is not shown.)

·iconLevel 字段:This valueindicates the current level of a LevelListDrawable that is

  used for the notification icon. You cananimate the icon in the status bar by changing

  this value to correlate with thedrawable's defined in a LevelListDrawable.

·contentView 字段:To define your ownlayout for the expanded message, instantiate a

  RemoteViews object and pass it to thecontentView field of your Notification. Pass the

  PendingIntent to the contentIntentfield.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值