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:当媒体扫描器完成扫描时发送。
相关推荐
MediaTea1 小时前
思考与练习(第四章 程序组成与输入输出)
java·linux·服务器·前端·javascript
前端老白1 小时前
webview在微信小程序中,安卓加载失败,IOS正常加载
android·ios·微信小程序·webview
2501_937154931 小时前
适配中兴主流机型 纯净版刷机固件技术优势合集
android·源码·源代码管理·机顶盒
2501_915106322 小时前
用 HBuilder 上架 iOS 应用时如何管理Bundle ID、证书与描述文件
android·ios·小程序·https·uni-app·iphone·webview
松涛和鸣2 小时前
35、Linux IPC进阶:信号与System V共享内存
linux·运维·服务器·数据库·算法·list
惊鸿一博2 小时前
Linux文件同步/镜像—rsync
linux·运维
SunnyDays10112 小时前
Python 实现 PDF 文档压缩:完整指南
linux·开发语言·python
TheNextByte12 小时前
如何通过OTG或不使用OTG将文件从Android传到U盘
android
2501_915909062 小时前
资源文件混淆在 iOS 应用安全中的实际价值
android·安全·ios·小程序·uni-app·iphone·webview
2501_915918412 小时前
iOS App 性能测试中常被忽略的运行期问题
android·ios·小程序·https·uni-app·iphone·webview