Home

Wisim

08 Aug 2014

BroadcastReceiver 的简单使用

和Activity以及 Service一样,BroadcastReceiver同属Android的四大组件。

有时候用 BroadcastReceiver 可以轻易解决很纠结的问题。

1.继承BroadcastReceiver实现一个广播接受者MyReceiver

可以重写onReceive(Context context,Intent intent)方法,在方法里面通过intent.getExtras()方法获取intenet中传递过来的数据

2.注册广播,有两种方式

(1).动态广播:通过context中的registerRecier方法来注册

MyReceiver myReceiver=new MyReceiver();
IntentFilter intentFilter=new IntentFilter("com.wxp.receiver.MyReceiver");
registerReceiver(myReceiver,intentFilter);

对于动态广播来说,如果Context被销毁时记得要注销广播:

if(myReceiver!=null) {
  unregisterReceiver(myReceiver);
}

(2).静态广播:在AndroidManifest.xml文件中注册

<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="com.wxp.receiver.MyReceiver" />
    </intent-filter>
</receiver>

3.发送广播

Intent intent =new Intent();
intent.setAction("com.wxp.receiver.MyReceiver");
intent.putExtra("msg","send by broadcast");
sendBroadcast(intent);

THE END.