I'm looking for a way of executing a piece of code within my Android application when the phone reaches 10% battery life. Would anyone be able to point me in the right direction of how to do so?

- 15,176
- 7
- 58
- 83

- 2,686
- 4
- 33
- 69
-
1Add `BroadcastReceiver` and `Services` in your application and check for battery level – Himanshu Agarwal Jan 12 '15 at 12:29
-
make background service which check battery level and when battery level reach certain point then fire your code with the help of broadcast receiver ... – duggu Jan 12 '15 at 12:30
-
doesn't need to register "broadcast receiver" as battery manager is sticky intent. – Murtaza Khursheed Hussain Jan 12 '15 at 12:32
-
Okay lots of difference answers here now and people saying different things. Could someone please clear things up for me, what is the correct way? – Paul Alexander Jan 12 '15 at 13:09
3 Answers
Get battery level in percentage
Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
float levelPert;
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
// Error checking that probably isn't needed but I added just in case.
if(level == -1 || scale == -1) {
levelPert = 50.0f;
}
levelPert = ((float)level / (float)scale) * 100.0f;
P.S. As mentioned in the documentation that BatteryManager is sticky intent, so there is no need to add manifest declaration.
You can read more about getting Battery details here

- 15,176
- 7
- 58
- 83
From developer.android.com :
You can't easily continually monitor the battery state, but you don't need to.
Generally speaking, the impact of constantly monitoring the battery level has a greater impact on the battery than your app's normal behavior, so it's good practice to only monitor significant changes in battery level—specifically when the device enters or exits a low battery state.
From this link

- 247
- 2
- 7
Write one receiver for getting the battery level like this:
public class BatteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
if (level < 10) {
// Write your code here
}
}
}
Write One Service:
public class BatteryService extends Service {
private BatteryReceiver receiver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
receiver = new BatteryReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
Register this service in your Manifest file like this:
<service android:name="com.example.testandroid.BatteryService" >
</service>
then start the service in First Activity is Enough.

- 2,567
- 2
- 17
- 24
-
-
@CommonsWare but in android docs they told intent actions can be register via manifest http://developer.android.com/reference/android/content/Intent.html – Rathan Kumar Jan 12 '15 at 13:07
-
Most can. `ACTION_BATTERY_CHANGED` cannot. http://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED – CommonsWare Jan 12 '15 at 13:22
-
@CommonsWare Thank you. but in the first link which i sent in that one they said like you can receive broadcast even though you registered in Manifest that's why i confused. Thank you Very much. – Rathan Kumar Jan 12 '15 at 13:26
-
-
If u want globally to the application use my solution, if u want locally with in activity take one inner class (broadcast receiver) and register with in activity is the best way.. – Rathan Kumar Jan 12 '15 at 14:28