Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a system variable that holds the device-specific low-battery threshold for Android?
    primarykey
    data
    text
    <p>I am checking to see if the battery has reached critical level. </p> <p>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).</p> <p>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).</p> <p>Doing a search, I found: <a href="https://stackoverflow.com/questions/11970185/when-android-fires-action-battery-low/11970715#11970715">When android fires ACTION_BATTERY_LOW</a>, 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).</p> <p>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.</p> <p>Here is my code:</p> <pre><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 &lt;= com.android.internal.R.integer.config_lowBatteryWarningLevel) { _thread.onBatteryStateReceived(DataModel.BatteryState.LOW); } } }; </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload