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&gt ... ><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);


更多相关文章

  1. Android实现ListView数据动态加载的方法
  2. Android动态设置主题样式
  3. Android6.0源码开发之修改默认音量default及max和min
  4. Android(安卓)wifi连接
  5. Android(安卓)4.4 KitKat NotificationManagerService使用详解与
  6. Android通过PendingIntent实现消息通知
  7. android 5.0以上通知栏、状态栏图标变成白色
  8. Android超人气系列动态壁纸下载(免费)
  9. 【原创】如何在Android中为TextView动态设置drawableLeft等

随机推荐

  1. Android中Message机制的灵活应用
  2. TextView添加ClickableSpan和LinkMovemen
  3. android java开发 第一天 之熟悉eclipse
  4. Android中SQLite的使用
  5. Could not open: C:\Documents and Sett
  6. Java(Android)线程池
  7. Android(安卓)开机自启动应用
  8. Android(安卓)多线程
  9. Android(安卓)AudioFlinger
  10. Android复制粘贴