Android--------------BroadcastReceiver的学习

从来好事天生俭,自古瓜儿苦后甜。这篇文章主要讲述Android--------------BroadcastReceiver的学习相关的知识,希望能为你提供帮助。
一、广播的注册方式
  发送广播:
  Intent mIntent = new Intent("com.simware.BroadcastReceiverDemo");

mIntent.putExtra("message", "hahahahaha"); sendBroadcast(mIntent);


【Android--------------BroadcastReceiver的学习】  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的学习

文章图片


    推荐阅读