7

在我的应用程序中,我想在电池电量不足时做一些事情。当电池电量不足时,android 会触发ACTION_BATTERY_LOW并且当电池再次达到其良好的健康状态时,它会触发 Intent ACTION_BATTERY_OKAY。所以,我对此有三个问题:

1.Android实际触发的电池百分比是ACTION_BATTERY_LOW多少?

2.如果电池电量更低,它会重复触发相同的事件吗?

3.我们可以配置android触发ACTION_BATTERY_LOW意图的电池百分比吗?

我更关心第三点。

4

3 回答 3

12

不,您无法设置何时发送 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);
于 2012-08-15T14:03:21.530 回答
7

该意图是从 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>

除非您正在开发自定义硬件平台,否则我不会对这些值的设置做出任何假设。

于 2012-08-15T14:01:09.350 回答
0

还有另一种方法可以从“com.android.internal.R.integer”字段中检测“config_lowBatteryWarningLevel”。

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();

    }
于 2018-03-22T08:57:35.400 回答