2

I am checking to see if the battery has reached critical level.

Android sends an intent to your app when the battery crosses the low-battery threshold in either direction. But this only works if the threshold is crossed while your app is running (the intent is not sticky, so it doesn't hang around). So if it's low when the user opens the app, you're out of luck (or at least information).

There is also a sticky intent, ACTION_BATTERY_CHANGED, that has information about the battery level and a scale for calculating percentages, which is great. However, I have been unable to find the system variable that contains the low-battery threshold (it apparently varies across device).

Doing a search, I found: When android fires ACTION_BATTERY_LOW, a source listing of Android system code, which uses the system variable com.android.internal.R.integer.config_lowBatteryWarningLevel. However, I have been unable to access this variable myself (my guess is that it is protected).

I would like to have a reasonable standard to compare my battery percentage to, so I know when to turn off battery-intensive functionality. That is all.

Here is my code:

private BroadcastReceiver powerListener = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
    int batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    int batteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
        int batteryPercentLeft = (batteryLevel * 100) / batteryScale;

        if (batteryPercentLeft <= com.android.internal.R.integer.config_lowBatteryWarningLevel) {
            _thread.onBatteryStateReceived(DataModel.BatteryState.LOW);
        }
    }
};

I get a compile error for the system variable. Is there an alternative? It seems like this should be a straightforward thing to do. I just want to match system behavior, nothing fancy.

2
  • It seems that it is not possible, see stackoverflow.com/questions/11970185/…
    – jhole
    Apr 23, 2013 at 20:10
  • Thanks. I did see that post when I was researching. But it says nothing about whether there is a system variable with the threshold (that part of the question was not answered). I am not trying to change the threshold. I just want to know what it is so I can make an intelligent test.
    – BeccaP
    Apr 23, 2013 at 20:50

1 Answer 1

0

Note that this is possible, but uses reflection so should not be relied upon:

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

Source: https://stackoverflow.com/a/49424298/608312

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.