当机器人火灾ACTION_BATTERY_LOW(When android fires ACTION

2019-06-27 01:49发布

在我的应用程序,我想要做的事,当电池电量低。 当电池电量低的Android火灾ACTION_BATTERY_LOW当电池再次达到其良好的健康它激发意图ACTION_BATTERY_OKAY 。 所以,我有这方面的三个问题:

1.At什么电池百分比的Android实际上触发ACTION_BATTERY_LOW

2.Does它多次大火,同样的事件如果电池电量甚至更低?

3,可我们配置的电量百分比,在其Android将火ACTION_BATTERY_LOW意图是什么?

我更关注的是第三点。

Answer 1:

不,你不能当ACTION_BATTERY_LOW门槛将被发送设置。 这是由Android的ROM指定一个系统级的意图。 这里是它设置在电池服务值的代码:

mLowBatteryWarningLevel = mContext.getResources().getInteger(
            com.android.internal.R.integer.config_lowBatteryWarningLevel);

见下文这是从在电池服务的更新方法Android系统代码切的代码:

/* The ACTION_BATTERY_LOW broadcast is sent in these situations:
         * - is just un-plugged (previously was plugged) and battery level is
         *   less than or equal to WARNING, or
         * - is not plugged and battery level falls to WARNING boundary
         *   (becomes <= mLowBatteryWarningLevel).
         */
        final boolean sendBatteryLow = !plugged
            && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
            && mBatteryLevel <= mLowBatteryWarningLevel
            && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);

        sendIntent();

        // Separate broadcast is sent for power connected / not connected
        // since the standard intent will not wake any applications and some
        // applications may want to have smart behavior based on this.
        Intent statusIntent = new Intent();
        statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
        if (mPlugType != 0 && mLastPlugType == 0) {
            statusIntent.setAction(Intent.ACTION_POWER_CONNECTED);
            mContext.sendBroadcast(statusIntent);
        }
        else if (mPlugType == 0 && mLastPlugType != 0) {
            statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED);
            mContext.sendBroadcast(statusIntent);
        }

        if (sendBatteryLow) {
            mSentLowBatteryBroadcast = true;
            statusIntent.setAction(Intent.ACTION_BATTERY_LOW);
            mContext.sendBroadcast(statusIntent);


Answer 2:

这意图从BatteryService解雇。 你必须对代码进行分析了一下,但我敢肯定它不会重复闪光:

http://gitorious.org/android-eeepc/base/blobs/fda6fae156e31a287e3cfbf66e51ea1405cdf479/services/java/com/android/server/BatteryService.java

它在火灾的实际值是在Android资源的设置,所以它仅在系统构建的配置。 这就是我们对我们的硬件,但是这很可能会为运行Android的每个硬件平台的不同:

<!-- Display low battery warning when battery level dips to this value -->
<integer name="config_lowBatteryWarningLevel">15</integer>

<!-- Close low battery warning when battery level reaches this value -->
<integer name="config_lowBatteryCloseWarningLevel">20</integer>

除非你正在开发一个定制的硬件平台上,我不会做什么,这些值设置为任何假设。



Answer 3:

还有就是检测“config_lowBatteryWarningLevel”,从“com.android.internal.R.integer”现场的另一种方式。

enter code here

    try {
        Class clazz = Class.forName("com.android.internal.R$integer");
        Field field = clazz.getDeclaredField("config_lowBatteryWarningLevel");
        field.setAccessible(true);
        int LowBatteryLevel = _context.getResources().getInteger(field.getInt(null));

        Log.d("LowBattery","warninglevel " + LowBatteryLevel);
    } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();

    }


文章来源: When android fires ACTION_BATTERY_LOW