BroadcastReceiver广播接收器

来源:互联网 发布:在淘宝上买气排安全不 编辑:程序博客网 时间:2024/05/16 10:53

1、Brodcast广播

是一种广泛运用在应用程序之间的传输信息的机制

2、BroadcastReceiver广播接收者

是对发送出来的广播进行过滤接收并响应的一系列组件,它就是接收来自系统和应用中的广播的

3、用途

–当开机完成后系统会产生一条广播

–当网络状态改变时系统会产生一条广播

–当电池电量改变时,系统会产生一条广播

–等等

使用方法

发送:

–把信息装入一个Intent对象(如Action、Category)

–通过调用响应的方法将Intent对象以广播方式发送出去

-sendBroadcast()-sendOrderBroadcast()-sendStickyBroadcast()

接收:

当Intent发送以后,所有已经注册的BroadcastReceiver会检查注册时的IntentFilter是否 与发送的Intent匹配,若匹配则就会调用BroadcastReceiver的onReceive()方法。所以当我们定义一个BroadcastReceiver的时候,都需要实现onReceive()方法。

注意!!!

-BroadcastReceiver生命周期只有几秒左右

-在BroadcastReceiver里不能做一些比较耗时的操作

-应该通过发送Intent给Service,有Service来完成

-不能使用子线程

普通广播特点:

-同级别接收是无序的

-级别低的后收到广播

-接收器不能截断广播的继续传播也不能处理广播

-同级别动态注册高于静态注册

无序广播特点:

-同级别接收顺序是无序的

-能截断广播的继续传播,高级别的广播接收器收到该广播后,可以决定是否把该广播截断

-接收器能截断广播的继续传播,也能处理广播

-同级别动态注册高于静态注册。

    public class BroadcastActivity extends AppCompatActivity implements View.OnClickListener{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_broadcast);        findViewById(R.id.send1).setOnClickListener(this);        findViewById(R.id.send2).setOnClickListener(this);        //动态注册广播        IntentFilter intentFilter = new IntentFilter("com.peanut.action.BROADCAST");        Receiver1 receiver1 = new Receiver1();        registerReceiver(receiver1,intentFilter);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.send1://发送一条普通广播                Intent intent = new Intent();                intent.setAction("com.peanut.action.BROADCAST");                intent.putExtra("msg","这是一条普通广播");                sendBroadcast(intent);            case R.id.send2://发送一条有序广播                Intent intent2 = new Intent();                intent2.setAction("com.peanut.action.BROADCAST");                intent2.putExtra("msg","这是一条有序广播");                sendOrderedBroadcast(intent2,null);        }    }}第一个接收器:    public class Receiver extends BroadcastReceiver{    @Override    public void onReceive(Context context, Intent intent) {        String s = intent.getStringExtra("msg");        Log.i("Receiver收到的有序广播:",s);        //abortBroadcast();截断广播,普通广播在接收器处无法截断        Bundle bundle = new Bundle();        bundle.putString("text","处理广播内容");        setResultExtras(bundle);        //abortBroadcast();截断广播继续传播    }}第二个接收器:    public class Receiver1 extends BroadcastReceiver{    @Override    public void onReceive(Context context, Intent intent) {        String s = intent.getStringExtra("msg");        Log.i("Receiver1收到的有序广播:",s);        Bundle bundle = getResultExtras(true);        String s1 = bundle.getString("text");        if (s1!=null){            Log.i("msg",s1);        }    }}<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/send1"        android:text="发送一条普通广播"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/send2"        android:text="发送一条有序广播"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>在AndroidManifest.xml里面注册<receiver android:name=".broadcast.Receiver">        <intent-filter>            <action android:name="com.peanut.action.BROADCAST"></action>        </intent-filter>    </receiver>
原创粉丝点击