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:当媒体扫描器完成扫描时发送。
相关推荐
feng_you_ying_li19 分钟前
linux之用户的权限详解(4)
linux·运维·服务器
二进制person1 小时前
JavaEE初阶 --网络编程
linux·服务器·网络
Cyber4K1 小时前
【妙招系列】Harbor 镜像私有仓库搭建手册
linux·云原生·容器
钛态2 小时前
Flutter 三方库 http_mock_adapter — 赋能鸿蒙应用开发的高效率网络接口 Mock 与自动化测试注入引擎(适配鸿蒙 HarmonyOS Next ohos)
android·网络协议·flutter·http·华为·中间件·harmonyos
王码码20352 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
左手厨刀右手茼蒿2 小时前
Flutter for OpenHarmony: Flutter 三方库 shamsi_date 助力鸿蒙应用精准适配波斯历法(中东出海必备)
android·flutter·ui·华为·自动化·harmonyos
Irissgwe2 小时前
进程间通信
linux·服务器·网络·c++·进程间通信
代码飞天2 小时前
wireshark的高级使用
android·java·wireshark
创世宇图3 小时前
阿里云Alibaba Cloud Linux 4 LTS 64位生产环境配置-Nginx
linux·nginx
2501_915918413 小时前
苹果App Store上架审核卡住原因分析与解决方案指南
android·ios·小程序·https·uni-app·iphone·webview