一、广播的注册方式
发送广播:
Intent mIntent = new Intent("com.simware.BroadcastReceiverDemo");
mIntent.putExtra("message", "hahahahaha");
sendBroadcast(mIntent);
BroadcastReceiver的静态注册----------------->就是在xml文件中加入
AndroidManifest.xml文件
<receiver android:name=".BroadCastRecevier">
<intent-filter >
<action android:name="com.simware.BroadcastReceiverDemo"/>
</intent-filter>
</receiver>
BroadCastRecevier.java文件
public class BroadCastRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("TAG", intent.getAction());
}
}
BroadcastReceiver的动态注册就是在接受的activity当中就行注册
@Override
protected void onCreate(Bundle savedInstanceState) {
//这步相当于静态注册的方法在onCreate中创建
IntentFilter filter = new IntentFilter();
filter.addAction(”com.simware.BroadcastReceiverDemo“);
registerReceiver(BroadCRecevier, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(BroadCRecevier);
}
private BroadcastReceiver BroadCRecevier = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
intent.getAction();
}
} ;
三、BroadcastReceiver生命周期----------》它的生命周期很短暂,只有不到10秒