android 进lanucher的广播,Android 添加自定义BOOT_COMPLETED广播避免延迟
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...
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)