7

私のアプリケーションでは、バッテリー残量が少ないときに何かをしたいと思っています。バッテリーが少なくなると、Androidが起動ACTION_BATTERY_LOWし、バッテリーが再び正常な状態に達すると、意図的に起動しACTION_BATTERY_OKAYます。だから、私はこれに関して3つの質問があります:

1.Androidが実際に起動するバッテリーの割合はどれくらいACTION_BATTERY_LOWですか?

2.バッテリーがさらに低下した場合、同じイベントが繰り返し発生しますか?

3.AndroidがACTION_BATTERY_LOWインテントを起動するバッテリーのパーセンテージを設定できますか?

私は3番目の点についてもっと心配しています。

4

3 に答える 3

12

いいえ、ACTION_BATTERY_LOWしきい値がいつ送信されるかを設定することはできません。これは、AndroidROMによって指定されるシステムレベルのインテントです。バッテリーサービスで値を設定するコードは次のとおりです。

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 に答える