Android--------------BroadcastReceiver的学习
Posted zhang11111wei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android--------------BroadcastReceiver的学习相关的知识,希望对你有一定的参考价值。
一、广播的注册方式
发送广播:
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秒
以上是关于Android--------------BroadcastReceiver的学习的主要内容,如果未能解决你的问题,请参考以下文章