在Android中,Broadcast是一种广泛运用在应用程序之间传输信息的机制,而BroadcastReceiver是对发送出来的Broadcast进行过滤接收并响应的一类组件
注册BroadcastReceiver
在AndroidManifest.xml中用标签注册
1 2 3 4 5
| <receiver android:name="myRecevice"> <intent-filer> <action android:name="com.zoe.net"/> </intent-filer> </receiver>
|
1 2 3
| IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(String); registerReceiver(BroadcastReceiver,intentFilter);
|
触发BroadcastReceiver
1 2 3
| Intent intent = new Intent("com.zoe.net"); intent.putExtra("msg","发送消息"); Content.sendBroadcast(intent);
|
接受到广播消息后会执行onReceive()方法
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Override public void onReceive(Context context,Intent intent) { String msg = intent.getStringExtra("msg"); int id = intent.getIntExtra("who",0); if(intent.getAction().equals("com.zoe.sendMsg")) { mn = (NotificationManage)context.getSystemService(Context.NOTIFICATION_SERViCE); notification = new Notification(R.drawable.icon,id+"发送广播",System.currentTimeMillis()); Intent it = new Intent(context,Main.class); PendIntent contentIntent = PenIntent.getActivity(context,0,it,0); notification.sendLatesEventInfo(context,"msg","msg",contentIntent); mn.notify(0,notification); } }
|