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()中取消注册。
private IntentReceiver mDataReceiver = new IntentReceiver() { @Override public void onReceiveIntent(Context context, Intent intent) { Log.d("hogeRecv","onReceiveIntent: "+intent); } } public void onResume() { super.onResume(); String action = "cn.com.chen.TestReceiver"; IntentFilter filter = new IntentFilter(action); this.registerReceiver(mDataReceiver,filter); } public void onPause() { super.onPause(); unregisterReceiver(mDataReceiver); }
- 静态声明同Service和Activity的声明类似。
<application> ... > <receiver name="receiver类名"> <intent-filter> <action name="cn.com.chen.TestReceiver"> </intent-filter> </receiver> </application>
在onReceive()中,不能直接同Activity交互,仅可Toast显示一个简单的通知消息、或通过NotificationManager设置一个Notification。对于Service,可以通过Context.startService()启动,但不能用Context.bindService()来绑定。如果需要同一个已启动的Service异步通信,可以通过BroadcastReceiver.peekService (Context myContext, Intent service)(返回值为IBinder)。
例:
IBinder binder = peekService(ctx, new Intent(ctx, MediaPlaybackService.class));
IMediaPlaybackService sService = IMediaPlaybackService.Stub.asInterface(binder);
Context.sendBroadcast()是一个完全异步处理,该函数调用后,其所能匹配的BroadcastReceiver则同时执行。而Context.sendOrderedBroadcast()则调用一次则能执行一个匹配的BroadcastReceiver,执行顺序只有IntentFilter的android:priority属性决定。
2. NotificationManager
Notification可以有多种形式,比如在StatusBar上的一个icon(通过该Icon可以启动一个Activity等)、LED闪烁、Screen的背景灯闪烁、震动等。用户拖动StatusBar,可以使Notification扩展显示为一个List UI。如果选择一个Nofitication,则触发该Nofitication所关联的PendingIntent所定义的行为。PendingIntent可以包含启动一个Activity、BroadcastReceiver、Service等信息。
int icon = R.drawable.notification_icon; // StatusBar上的图标
CharSequence tickerText = "Hello"; // 启动时,在StatusBar上显示的走马灯
long when = System.currentTimeMillis(); // Notification启动时间
Context CharSequence contentTitle = "My notification"; // (扩展显示时)Notification的标题
CharSequence contentText = "Hello World!"; // (扩展显示时)Notification的消息
Notification notification = new Notification(icon, tickerText, when);
Intent notificationIntent = new Intent(this, MyActivity.class); // 后续的PendingIntent用
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Context context = getApplicationContext();
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
此外,还可以通过设置Notification的属性来设置提示音、震动等。
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);