注册广播共有两种方式,一种是通过在manifest.xml文件中注册, 不需要手动进行注销
先在XML文件中添加
<receiver android:name="Broadcastreceiver">
<intent-filter>
<action android:name="test" />
</intent-filter>
</receiver>
然后再在JAVA篇码段中添加Intent intent = new Intent("test"); sendBroadcast(intent);
另一种直接在代码中通过代码注册(有点类似如隐式Intent);需要通过手动进行注销(unregisterReceiver(receiver);
首先定义IntentFilter intentfilter = new IntentFilter("test");
registerReceiver(receiver, intentfilter);
unregisterReceiver(receiver);
然后再接收
Broadcastreceiver_test receiver = new Broadcastreceiver_test(){
public void onReceive(android.content.Context context, Intent intent) {
log.d("receiver","receiver:"+test); };
最后发送Intent intent = new Intent("test"); sendBroadcast(intent);