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:当媒体扫描器完成扫描时发送。
相关推荐
Kapaseker2 小时前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴3 小时前
Android17 为什么重写 MessageQueue
android
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
xq95271 天前
Andorid Google 登录接入文档
android
黄林晴1 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移