• Android学习笔记 31. Receiver组件


    Android学习笔记

    Android基础开发——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) {
            
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    在清单文件中注册

    <!--    静态注册广播接收者    -->
    <receiver android:name=".CustomReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="com.dingjiaxiong.study"/>
        </intent-filter>
    </receiver>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    创建一个接口

    package com.dingjiaxiong.myreceiver;
    
    public interface ActionUtils {
    
        // 广播注册时 与发送广播时 的唯一标识 , 必须保持一致(动态注册用)
    
        String ACTION_EQUES_UPDATE_IP = "com.dingjiaxiong.receiver_study_";
    
        // 广播注册时 与发送广播时 的唯一标识 , 必须保持一致(静态注册用)
    
        String ACTION_FLAG = "com.dingjiaxiong.receiver_flag_";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    改一下清单文件

    在这里插入图片描述

    修改接收者类,打印日志

    在这里插入图片描述

    修改布局

    
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    在这里插入图片描述

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    运行

    在这里插入图片描述

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

    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 广播接收者 " );
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    使用java代码注册广播

    在这里插入图片描述

    发送

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

    在这里插入图片描述

    运行

    在这里插入图片描述

    成功。

  • 相关阅读:
    x86-7-页式管理(Paging)
    FP为什么更适合做独立站?自己掌握话语权才是王道
    前端总结——WebSocket
    MySQL数据库基础知识
    springboot项目讲解
    国内常用的代理ip形式数据中心代理ip和静态住宅代理IP有什么区别
    具备什么价值的安全数据交换系统,是企业真正需要的?
    智能搬运小车(自动抓取、循迹)
    【Java 进阶篇】插上翅膀:JQuery 插件机制详解
    Linux系统下进程的概念《一》
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126278002