android 进lanucher的广播,Android 添加自定义BOOT_COMPLETED广播避免延迟

本文探讨了在Android设备上,标准的BOOT_COMPLETED广播延迟接收的问题,提出通过自定义广播在系统启动初期发送,避免数据丢失。作者分享了如何在SystemServer.java中实现这一功能,以确保应用程序的初始化及时准确。
摘要由CSDN通过智能技术生成

Android 有自己的开机广播:

/**

* Broadcast Action: This is broadcast once, after the system has finished

* booting. It can be used to perform application-specific initialization,

* such as installing alarms. You must hold the

* {@link android.Manifest.permission#RECEIVE_BOOT_COMPLETED} permission

* in order to receive this broadcast.

*

*

This is a protected intent that can only be sent

* by the system.

*/

@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)

public static final String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";

但是在接触的一台设备上,这个广播在进入Launcher之后45秒才收到。明显慢了很多。

通过节点去控制设备的开光状态,节点在关机重启时会被重置为默认状态,这就意味着45s之后才能给节点设为用户上次的设置的值。在这之前的一些数据会丢失或错误计算。

所以只能添加一个自定义的开机广播,当系统启动完成即刻发出。但是要注意的是,此时并非真正意义上的“BOOT_COMPLETED”,也就是说系统的一些状态还没有初始化成功,比如IMEI号,此时去获取的话返回的是null,但是启动服务去执行自己的操作还是可以的。

添加方法:

diff --git a/kitkat/frameworks/base/services/java/com/android/server/SystemServer.java b/kitkat/frameworks/base/services/java/com/andr

index b61b9f3..eac817b 100644

--- a/kitkat/frameworks/base/services/java/com/android/server/SystemServer.java

+++ b/kitkat/frameworks/base/services/java/com/android/server/SystemServer.java

@@ -983,6 +983,7 @@ class ServerThread {

if (!headless) {

startSystemUi(contextF);

//startSimStateChangeService(contextF);

+ sendCustomBootCompleted(contextF);

}

try {

if (mountServiceF != null) mountServiceF.systemReady();

@@ -1146,6 +1147,18 @@ class ServerThread {

Slog.d("SimStateChange", "Starting service: " + intent);

context.startServiceAsUser(intent, UserHandle.OWNER);

}

+

+ static final void sendCustomBootCompleted(Context context){

+ try {

+ Intent intent = new Intent();

+ intent.setAction("test.intent.action.BOOT_COMPLETED");

+ context.sendBroadcast(intent);

+ System.out.println("send custom broadcast");

+ } catch (Exception e) {

+ e.printStackTrace();

+ System.out.println("Unable to send custom broadcast");

+ }

+ }

} Okay,大功告成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值