简体   繁体   English

检查 BOOT_COMPLETED 是否已经在 Android 上被触发

[英]Check if BOOT_COMPLETED has been fired already on Android

I have a Broadcast Receiver which handles multiple events.我有一个处理多个事件的广播接收器。 I need it to do some special things for me at boot so I registered the android.intent.action.BOOT_COMPLETED intent, which works fine.我需要它在启动时为我做一些特殊的事情,所以我注册了android.intent.action.BOOT_COMPLETED意图,它工作正常。 If the device is plugged in and is charging the android.intent.action.ACTION_POWER_CONNECTED intent is fired before BOOT_COMPLETED and does work before it's supposed to do stuff.如果设备已插入并正在充电,则android.intent.action.ACTION_POWER_CONNECTED意图在BOOT_COMPLETED之前被触发,并在它应该做的事情之前工作。 (I'm using BOOT_COMPLETED as a sort of initializer). (我使用BOOT_COMPLETED作为一种初始化程序)。

Is there a way to check if the BOOT_COMPLETED event has been fired, so that I can run my initializing code in the event something gets fired too early?有没有办法检查BOOT_COMPLETED事件是否已被触发,以便我可以在过早触发某些事件时运行我的初始化代码?

When the BOOT_COMPLETED is fired, you need to set a flag in your SharedPreferences as "true".BOOT_COMPLETED被触发时,您需要在 SharedPreferences 中将一个标志设置为“true”。

So, now if your ACTION_POWER_CONNECTED gets fired before the BOOT_COMPLETED then you need to check the value of the flag from the SharedPreferences.因此,现在如果您的ACTION_POWER_CONNECTEDBOOT_COMPLETED之前被触发,那么您需要检查 SharedPreferences 中标志的值。

If the value is still false, that means that the BOOT_COMPLETED broadcast has not fired yet and you should not perform your actions.如果该值仍然为 false,则表示 BOOT_COMPLETED 广播尚未触发,您不应执行您的操作。

Note - Also remember to reset the flag in SharedPreferences every time.注意 - 还要记住每次都重置 SharedPreferences 中的标志

I found another solution which is consistent.我找到了另一个一致的解决方案。 (I couldn't use another approach as I'm not the one who decides the approach sadly.) (我不能使用另一种方法,因为我不是可悲地决定方法的人。)

Using the answer from this post: How to get Android system boot time I can save the last boot time in SharedPreferences (or some other storage).使用这篇文章的答案: How to get Android system boot time我可以将上次启动时间保存在 SharedPreferences(或其他一些存储)中。 Once the boot time is different than the last I know that it's a fresh boot and can apply my initialisations etc.一旦启动时间与上次不同,我就知道这是一个全新的启动,可以应用我的初始化等。

This is the code I've ended up writing:这是我最终编写的代码:

private boolean checkIfRebooted(Context context) {
    SharedPreferences prefs = context.getSharedPreferences("package.name.class", Context.MODE_PRIVATE);

    long savedUptime = prefs.getLong("timestamp", 0);
    long currentUptime = System.currentTimeMillis() - SystemClock.elapsedRealtime();

    // Giving it a threshold of 10ms since the calculation may be off by one sometimes.
    if (Math.abs(currentUptime - savedUptime) < 10)
        return false;

    prefs.edit().putLong("timestamp", currentUptime).apply();
    return true;
}

You could use the kernel boot id from /proc/sys/kernel/random/boot_id.您可以使用 /proc/sys/kernel/random/boot_id 中的内核引导 ID。 When the code receives BOOT_COMPLETED, store the boot_id in the shared preference.当代码收到 BOOT_COMPLETED 时,将 boot_id 存储在共享首选项中。 After that it is simple.之后就简单了。 If the boot_id from /proc is equal to the shared preference, then BOOT_COMPLETED has been handled.如果来自 /proc 的 boot_id 等于共享首选项,则 BOOT_COMPLETED 已被处理。 Otherwise, not yet.否则,还没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM