首页 > 移动平台 > 详细

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

时间:2018-11-07 00:26:15      阅读:192      评论:0      收藏:0      [点我收藏+]

一、BroadcastReceiver的静态注册

  BroadCastRecevier.java文件

  public class BroadCastRecevier extends BroadcastReceiver {
   @Override
  public void onReceive(Context context, Intent intent) {
   Log.i("TAG", intent.getAction());
  }
}

  AndroidManifest.xml文件

  <receiver android:name=".BroadCastRecevier">
  <intent-filter >
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
  </intent-filter>
  </receiver>

二、BroadcastReceiver的动态注册

  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

 

  //这步相当于静态注册的方法
  IntentFilter filter = new IntentFilter();
  filter.addAction(Intent.ACTION_APP_ERROR);
  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的学习

原文:https://www.cnblogs.com/liunx1109/p/9919805.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!