四大组件---BroadcastReceiver

1.Broadcast(广播)是一种应用之间传输信息的机制

2.BroadcastReceiver(广播接收者)是对广播过滤接收并响应的一类组件

发送广播:

1.把信息装入Intent对象(如Action、Category)

2.通过调用相应方法将Intent对象以广播的方式发送出去:

<1.sendBroadcast()

<2.sendOrderBroadcast()

<3.sendStickyBroadcast()

接收广播:

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

tip:<1.BroadcastRecevier需要注册(静态注册,代码注册)

       <2.由于BroadcastReceiver的生命周期只有十秒左右,在BroadcastReceiver里不能做耗时操作(同理也不能使用子线程),应该通过发送Intent给Service,由Service完成。

广播的种类:

1.普通广播(Normal Broadcast):所有监听该广播的广播接收者都能够监听到该广播。

<1.同级别接收先后是随机的(无序)

<2.级别低的后收到广播

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

<4.同级别动态注册高于静态注册

<5.该方式效率更高,但是不能使用setResult、getResult、abort系列的API

2.有序广播(Orderd Broadcast):按照接收者的优先级顺序接收广播,优先级别在intent-filter中的priority中声明(-1000到1000之间,值越大优先级越高),可以终止广播意图的继续传播,接收者可以更改其内容。

<1.同级别接收顺序是随机的(高级的比低级优先执行)

<2.能截断广播的继续传播,高级别的广播接收者接收到广播后可以决定是否截断该广播

<3.处理广播

<4.同级别动态注册高于静态注册

<5.若多个应用的优先级相同则先注册广播的优先接收广播

3.异步广播(粘滞性滞留广播):不能将处理结果传给下一个接收者,无法终止广播。

Demo1(发送接收一条普通广播):

Mainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.broadcastreceiverdemo">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".BC1">
            <intent-filter
                android:priority="100">
                <action android:name="BC_One"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".BC2">
            <intent-filter
                android:priority="200">
                <action android:name="BC_One"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>
BC1和BC2在此处静态注册并声明BC2优先级200,BC1优先级100


BC1.java
public class BC1 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String s = intent.getStringExtra("msg");
        System.out.println("receiver1接收到消息:"+s);
    }
}
BC2.java
public class BC2 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String s = intent.getStringExtra("msg");
        System.out.println("receiver2接收到消息:"+s);
    }
}

BC3.java
public class BC3 extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        String s = intent.getStringExtra("msg");
        System.out.println("receiver3接收到消息:"+s);
    }
}



ActivityMain.java

package com.example.administrator.broadcastreceiverdemo;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFilter iF = new IntentFilter("BC_One");
        BC3 bc3 = new  BC3();
        registerReceiver(bc3,iF);
    }
    public void Click(View v) {
        switch (v.getId()) {
            case R.id.send1: //发送一条普通广播
                Intent intent = new Intent();
                intent.putExtra("msg","一条普通广播");
                intent.setAction("BC_One");//为保证唯一性建议写成包名+字段
                sendBroadcast(intent);
                break;
        }
    }
}

—————————————————————————————————————————————————————————————————————————————

动态注册BC3:

IntentFilter iF = new IntentFilter("BC_One");
BC3 bc3 = new  BC3();
registerReceiver(bc3,iF);

动态注册广播在组件结束后需要销毁:

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
}

未设置BC1与BC2优先级时几乎同时收到广播

BC2优先级大于BC1时,BC2先收到广播

动态注册BC3优先静态注册BC2,BC1收到广播

tip:动态注册存在缺陷,只有在当前程序运行时才生效,若程序终止则可能接收不到广播。

有序广播:

MainActivity

case R.id.send2://发送一条有序广播
    Intent intent2 = new Intent();
    intent2.putExtra("msg","这是一条有序广播");
    intent2.setAction("BC_One");
    sendOrderedBroadcast(intent2,null);


bc2

public class BC2 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String s = intent.getStringExtra("msg");
        System.out.println("receiver2接收到消息:"+s);
        Bundle bundle = new Bundle();
        bundle.putString("test","广播处理的数据");
        setResultExtras(bundle);
    }
}


bc1

public class BC1 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String s = intent.getStringExtra("msg");
        System.out.println("receiver1接收到消息:"+s);
        Bundle bundle = getResultExtras(true);
        String s2 = bundle.getString("test");
        System.out.println(s2);
    }
}


bc2处理广播后bc1接收


终止广播:

abortBroadcast();


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值