Android学习-BroadcastReceiver

简介
1>Broadcast(广播)
是一种广泛运用在应用程序之间传输信息的机制。
2>BroadcastReceiver(广播接收者)
是对发送出来的广播进行过滤接收并响应的一类组件,他就是用来接受来自系统和应用中的广播
3>用途:
-当开机完成后系统会产生一条广播
-当网络状态改变时会产生一条广播
-当电池电量改变时,系统会产生一条广播

使用方法:
——发送:
*把信息装入一个Intent对象(如Action、Category)
*通过调用相应的方法将Intent对象以广播方式发送出去
sendBroadcast()
sendOrderBroadcast()
sendStickyBroadcast()
——接收
当Intent发送以后,所有已经注册的BroadcastReceiver会检查注册时的IntentFilter是否与发送的Intent相匹配,若匹配则会调用BroadcastReceiver的onReceive()方法。所以当我们定义一个BroadcastReceiver的时候,都需要实现onReceive()方法。
【注意】:
BroadcastReceiver生命周期只有十秒左右
在BroadcastReceiver里不能做一些比较耗时的操作
应该通过发送Intent给Service,让Service去完成。
不能使用子线程。

广播的种类
-普通广播
所有监听该广播的广播接收者都可以监听该广播
-有序广播
按照接收者的优先级顺序接收广播,优先级在intent-filter中的priority声明,-1000到1000之间,值越大,优先级越高。可以终止广播意图的继续传播。接收者可以篡改内容。
-异步广播(粘滞性滞留广播)
不能将处理结果传给下一个接收者,无法终止广播。

普通广播特点:
同级别接收先后是随机的
级别低的后收到广播
接收器不能截断广播的继续传播也不能处理广播
同级别动态注册属于静态注册
有序广播特点:
同级别接收顺序是随机的
能截断广播的继续传播,高级别的广播接收器收到该广播后,可以决定把该广播是否截断。
接收器能截断广播的继续传播,也能处理广播。
同级别动态注册高于静态注册。

BC1广播接收器1

package com.example.angel.sqlitepro;
public class BC1 extends BroadcastReceiver{
    public void onReceive(Context context, Intent intent) {
        String s = intent.getStringExtra("msg");
        Log.i("tag", "!!!!!!接收器一号 "+s);
    }
}

BC2广播接收器2

package com.example.angel.sqlitepro;
public class BC2 extends BroadcastReceiver{
    public void onReceive(Context context, Intent intent) {
        String s = intent.getStringExtra("msg");
        Log.i("tag", "!!!!!!接收器二号 "+s);
    }
}

MainActivity.java

package com.example.angel.sqlitepro;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.nio.channels.InterruptedByTimeoutException;

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
    }
    public void doClick(View v){
        switch (v.getId()){
            case R.id.send1://发送一条普通广播
                Intent intent = new Intent();
                intent.putExtra("msg","这是一条普通广播");
                intent.setAction("BC_One");
                sendBroadcast(intent);
        }
    }

}

配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.angel.sqlitepro">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>
                <action android:name="BC_One"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".BC2">
            <intent-filter>
                <action android:name="BC_One"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

这里写图片描述

设置成优先级别的接收器

<receiver android:name=".BC1">
            <intent-filter android:priority="-500">
                <action android:name="BC_One"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".BC2">
            <intent-filter android:priority="500">
                <action android:name="BC_One"/>
            </intent-filter>
        </receiver>

这里写图片描述

动态接收广播(动态优先级大于静态)
先去掉BC2的静态全局设置

package com.example.angel.sqlitepro;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        IntentFilter intentFilter = new IntentFilter("BC_One");
        BC2 bc2 = new BC2();
        registerReceiver(bc2,intentFilter);

    }
    public void doClick(View v){
        switch (v.getId()){
            case R.id.send1://发送一条普通广播
                Intent intent = new Intent();
                intent.putExtra("msg","这是一条普通广播");
                intent.setAction("BC_One");
                sendBroadcast(intent);
        }
    }

}

这里写图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值