31. Receiver组件

31. Receiver组件

31.1 认识Receiver

广播:分为系统广播 与 用户自定义广播

系统广播:开机广播、网络状态广播、蓝牙…

31.2 静态注册接收广播

定义一个广播接收者

package com.dingjiaxiong.myactivitytiaozhuan;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

//接收者
public class CustomReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        
    }
}

在清单文件中注册

<!--    静态注册广播接收者    -->
<receiver android:name=".CustomReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="com.dingjiaxiong.study"/>
    </intent-filter>
</receiver>

在这里插入图片描述

创建一个接口

package com.dingjiaxiong.myreceiver;

public interface ActionUtils {

    // 广播注册时 与发送广播时 的唯一标识 , 必须保持一致(动态注册用)

    String ACTION_EQUES_UPDATE_IP = "com.dingjiaxiong.receiver_study_";

    // 广播注册时 与发送广播时 的唯一标识 , 必须保持一致(静态注册用)

    String ACTION_FLAG = "com.dingjiaxiong.receiver_flag_";
}

改一下清单文件

在这里插入图片描述

修改接收者类,打印日志

在这里插入图片描述

修改布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!-- 动态注册广播 发送广播 领域   -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动态注册广播 发送广播 区域"
        android:textSize="20dp"
        android:layout_gravity="center_horizontal"
        />
    
    <Button
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        android:onClick="sendAction1"
        />


    <!-- 静态注册广播 发送广播 领域   -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="静态注册广播 发送广播 区域"
        android:textSize="20dp"
        android:layout_gravity="center_horizontal"
        />

    <Button
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        android:onClick="sendAction2"
        />

</LinearLayout>

package com.dingjiaxiong.myreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
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);
    }

    public void sendAction1(View view) {
    }

    // 静态发送广播 给 接收者
    public void sendAction2(View view) {
        Intent intent = new Intent();
        intent.setAction(ActionUtils.ACTION_FLAG); //这里必须和注册时保持一致
        sendBroadcast(intent);
    }
}

在这里插入图片描述

运行

在这里插入图片描述

点击发送广播,可以看到报错了,开始谷歌

BroadcastQueue: Background execution not allowed: receiving Intent { act=com.usb.printer.USB_PERMISSION flg=0x10 (has extras) }

出现此报错的原因是Android O中对隐式广播做了限制,
在调试USB打印功能的时候,USB打印机是通过广播接收到打印指令数据进行打印的,在AndroidManifest.xml中使用静态注册的方式注册的广播,在Android O以上就会没有效果,日志出现BroadcastQueue的报错,把静态注册修改成在代码动态注册广播即可解决问题;

解决办法

在intent发送之前设置一下包名

那没事儿了

31.3 动态注册接收广播

定义广播接收者

package com.dingjiaxiong.myreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class UpdateIpSelectCity extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("dingjiaxiong", "UpdateIpSelectCity onReceive 广播接收者 " );
    }
}

在这里插入图片描述

使用java代码注册广播

在这里插入图片描述

发送

//动态发送广播 给 接收者
public void sendAction1(View view) {
    Intent intent = new Intent();
    intent.setAction(ActionUtils.ACTION_EQUES_UPDATE_IP); //这里必须和注册时保持一致
    sendBroadcast(intent);
}

在这里插入图片描述

运行

在这里插入图片描述

成功。

 

posted @ 2022-09-19 08:23  随遇而安==  阅读(23)  评论(0编辑  收藏  举报