首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android通报及receiver

2012-06-26 
Android通知及receiver?1. BroadcastReceiverBroadcastReceiver用来接收如电量低、信号弱等即时广播通知,发

Android通知及receiver

?

1. BroadcastReceiver

BroadcastReceiver用来接收如电量低、信号弱等即时广播通知,发送侧调用Context.sendBroadcast(Intent)或Context.sendOrderedBroadcast(Intent)可发送这类广播通知,而BroadcastReceiver则用onReceive (Context, Intent)方法来处理到接受到的广播通知。一个BroadcastReceiver的生命周期和onReceive()一致,其宿主进程和Activity、Service的一样,因此在onReceive不能有长时间的循环处理,不能显示Dialog的UI。


一个BroadcastReceiver可以在AndroidManifest.xml中静态声明和在程序动态注册,其可处理的通知由在声明或注册时定义的IntentFilter决定。

动态注册的例子: 如果在Activity中动态注册,必须在onResume()中注册,在onPause()中取消注册。
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;notification.ledOnMS = 300;   notification.ledOffMS = 1000;notification.flags |= Notification.FLAG_SHOW_LIGHTS; NotificationManager用来管理Notification,用Context.getSystemService(String)可从系统中获获取。NotificationManager的方法notify(int id, Notification)可以显示一个Notification到StatusBar上,方法cancel(int id)可删除一个Notification。参数ID用来标识Notification,在一个程序中必须唯一。String ns = Context.NOTIFICATION_SERVICE;NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);Notification notification = ...           // Notification设置mNotificationManager.notify(1, notification);
?

?

?

热点排行