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:当媒体扫描器完成扫描时发送。
相关推荐
WaiSaa16 分钟前
Linux内核升级
linux·运维
白如意i25 分钟前
在Ubuntu 16.04上安装和配置Elasticsearch的方法
linux·ubuntu·elasticsearch
夜色呦33 分钟前
深入 Laravel 的错误处理与异常处理机制
android·php·laravel
stackY、34 分钟前
【Linux】:命令行参数
linux
Feel_狗焕36 分钟前
Linux下GDB调试一篇入魂(GDB调试详解)
linux·debug
不知 不知1 小时前
CentOS中使用SSH远程登录
linux·centos·ssh
l258036911211 小时前
Keepalived
linux·运维
菜鸟赵大宝1 小时前
【Unix/Linux】$bash-3.2是什么
linux·unix
love6a61 小时前
从零开始学习网络安全渗透测试之Linux基础篇——(三)Linux文件与路径
linux·运维·学习
~我在北京~2 小时前
完整篇-Python数据类型转换
linux·windows·python