Android广播demo(系统广播,自定义广播)

1 系统广播demo

1.1 BootReceiver 的广播接收器类:

复制代码
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 检查广播动作是否是设备启动完成
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            // 在设备启动完成时显示 Toast 消息
            Toast.makeText(context, "设备已启动完成!", Toast.LENGTH_SHORT).show();
        }
    }
}

1.2 AndroidManifest.xml 文件中声明广播接收器,并指定接收 BOOT_COMPLETED 广播

复制代码
<receiver
    android:name=".BootReceiver"
    android:enabled="true"
    android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

1.3 应用程序已经请求了 RECEIVE_BOOT_COMPLETED 权限

复制代码
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

1.4 设备启动完成时,系统会发送 BOOT_COMPLETED 广播,而我们的广播接收器会接收到该广播并触发 onReceive() 方法,从而显示一个 Toast 消息

2 自定义广播demo

2.1 发送广播的活动

复制代码
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class SendBroadcastActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_broadcast);

        Button sendButton = findViewById(R.id.sendButton);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建一个自定义广播 Intent
                Intent customBroadcastIntent = new Intent("com.example.androiddemo.CUSTOM_BROADCAST");
                // 添加额外的数据
                customBroadcastIntent.putExtra("message", "这是自定义广播的消息!");
                // 发送广播
                sendBroadcast(customBroadcastIntent);
            }
        });
    }
}

2.2 发送广播的活动布局文件(activity_send_broadcast.xml)中,添加一个按钮

复制代码
<Button
    android:id="@+id/sendButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="发送自定义广播" />

2.3 AndroidManifest.xml 中声明了这两个活动

复制代码
<activity android:name=".SendBroadcastActivity" />
<activity android:name=".ReceiveBroadcastActivity" />

2.4 接收广播的活动

复制代码
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class ReceiveBroadcastActivity extends AppCompatActivity {

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 从广播中获取消息
            String message = intent.getStringExtra("message");
            // 显示消息
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        // 注册广播接收器
        registerReceiver(broadcastReceiver, new IntentFilter("com.example.androiddemo.CUSTOM_BROADCAST"));
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 取消注册广播接收器
        unregisterReceiver(broadcastReceiver);
    }
}

3 常用系统广播

  • BOOT_COMPLETED:系统启动完成后,
  • ACTION_POWER_CONNECTED:连接到电源时发送
  • ACTION_POWER_DISCONNECTED:当设备断开电源连接时发送,
  • ACTION_BATTERY_CHANGED:当设备电池状态发生变化时发送
  • ACTION_SCREEN_ON:当设备的屏幕被打开时发送。
  • ACTION_SCREEN_OFF:当设备的屏幕被关闭时发送。
  • ACTION_USER_PRESENT:当用户解锁设备时发送。
  • ACTION_TIME_TICK:每分钟发送一次的系统广播,用于触发定时任务。
  • ACTION_PACKAGE_ADDED:当应用程序被安装时发送。
  • ACTION_PACKAGE_REMOVED:当应用程序被卸载时发送。
  • ACTION_HEADSET_PLUG:当耳机被插入或拔出时发送。
  • ACTION_MEDIA_MOUNTED:当存储设备被挂载时发送。
  • ACTION_MEDIA_UNMOUNTED:当存储设备被卸载时发送。
  • ACTION_MEDIA_SCANNER_STARTED:当媒体扫描器开始扫描时发送。
  • ACTION_MEDIA_SCANNER_FINISHED:当媒体扫描器完成扫描时发送。
相关推荐
辞旧 lekkk38 分钟前
【Qt系统相关】鼠标事件
linux·开发语言·qt·学习·计算机外设·萌新
是摆烂第一名呀1 小时前
【rk3506】U-Boot串口时钟未配置,导致串口彻底失效
linux·arm开发·驱动开发·嵌入式硬件
鱼儿也有烦恼1 小时前
01.搭建Android Studio开发环境
android·android studio
liang89992 小时前
Anconda常用命令
linux·人工智能·conda
无足鸟ICT2 小时前
【RHCA+】管道符
linux·运维·服务器
老约家的可汗2 小时前
Linux的库制作和原理
linux·运维·服务器
m0_738120722 小时前
PHP代码审计基础——面向对象(四)
android·开发语言·网络·安全·github·php
非鱼䲆鱻䲜3 小时前
CCS 20.5.0的printf重定向串口怎么实现?
linux·单片机·电赛·ccs
左手厨刀右手茼蒿3 小时前
PCIe设备驱动开发:从枚举到DMA传输的完整链路
linux·嵌入式·系统内核
济6173 小时前
I.MX6U Linux 驱动开发篇---零基础必看!中断实验(按键中断 + 定时器消抖 + 设备树配置实战教程)--- Ubuntu20.04
linux·驱动开发·嵌入式·嵌入式linux驱动开发