当前位置: 移动技术网 > 移动技术>移动开发>Android > Android基础知识之broadcast广播详解

Android基础知识之broadcast广播详解

2019年07月24日  | 移动技术网移动技术  | 我要评论

android中的广播用的太多了,今天稍微总结一下。
按注册方式分为两种:

1.静态注册广播:
静态注册广播就是在androidmanifest.xml文件中注册广播,假设我们要实现这样一个效果,在一个activity上点击按钮,发送一条广播,这条广播弹出一个toast,显示“静态”二字。

先看看广播接受者:

public class mybroadcast extends broadcastreceiver {

  @override
  public void onreceive(context context, intent intent) {
    toast.maketext(context,"静态", toast.length_long).show();
  }

}

清单文件中注册:

  <receiver android:name="com.example.staticbroadcast.mybroadcast" >
      <intent-filter>
        <action android:name="com.test.staticbroadcast" />
      </intent-filter>
    </receiver>

activity中的点击事件(发送广播):

 this.static_btn.setonclicklistener(new onclicklistener() {

      @override
      public void onclick(view v) {
        intent intent = new intent();
        intent.setaction("com.test.staticbroadcast");
        sendbroadcast(intent);
      }
    });

2.动态注册:
动态注册一般是在activity中的onstart()方法中注册,在onstop()方法中解除注册,代码如下:

public class mainactivity extends activity {

  private button static_btn;
  private button dynamic_btn;
  private broadcastreceiver mybroadcastreceiver;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    this.static_btn = (button) this.findviewbyid(r.id.button1);
    this.dynamic_btn = (button) this.findviewbyid(r.id.button01);
    mybroadcastreceiver = new broadcastreceiver(){

      @override
      public void onreceive(context context, intent intent) {
        toast.maketext(mainactivity.this,"你好,这里是动态广播!", toast.length_long).show();
      }

    };

//   this.static_btn.setonclicklistener(new onclicklistener() {
//
//     @override
//     public void onclick(view v) {
//       intent intent = new intent();
//       intent.setaction("com.test.staticbroadcast");
//       sendbroadcast(intent);
//     }
//   });

    this.dynamic_btn.setonclicklistener(new onclicklistener() {

      @override
      public void onclick(view v) {
        //发送广播
        intent intent = new intent();
        intent.setaction("dynamicbroadcast");
        sendbroadcast(intent);
      }
    });
  }

  @override
  protected void onstart() {
    super.onstart();
    //注册广播
    intentfilter intentfilter = new intentfilter();
    intentfilter.addaction("dynamicbroadcast");
    registerreceiver(mybroadcastreceiver, intentfilter);

  }

  @override
  protected void onstop() {
    super.onstop();
    //取消注册
    unregisterreceiver(mybroadcastreceiver);
  }
}

关于静态注册的细节:
android:exported=”true”这个属性表示该广播接收器是否接收来自其他app发出的广播,如果有intent-filter属性,则默认为true,否则默认为false。

每个广播接收者都可以接受多个广播源,如果是静态注册,那么你要这么做:

  <receiver 
      android:exported="true"
      android:name="com.example.staticbroadcast.mybroadcast" >
      <intent-filter>
        <action android:name="com.test.staticbroadcast" />
        <action android:name="com.test.staticbroadcast2"/>
      </intent-filter>
    </receiver>

在广播接收器中这样处理:

  @override
  public void onreceive(context context, intent intent) {
    if (intent.getaction().equals("com.test.staticbroadcast")) {
      toast.maketext(context, "静态", toast.length_short).show();
    } else if (intent.getaction().equals("com.test.staticbroadcast2")) {
      toast.maketext(context, "静态2", toast.length_short).show();
    }
  }

如果是动态注册,注册方式如下:

  @override
  protected void onstart() {
    super.onstart();
    //注册广播
    intentfilter intentfilter = new intentfilter();
    intentfilter.addaction("dynamicbroadcast");
    intentfilter.addaction("dynamicbroadcast2");
    registerreceiver(mybroadcastreceiver, intentfilter);

  }

广播接收器中的处理方式与静态注册一致。

关于怎样使用broadcast实现activity和fragment之间的通信可以查看我的另一篇博客

原文地址:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网