3

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?

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
Paul Alexander
  • 2,686
  • 4
  • 33
  • 69

3 Answers3

1

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

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
1

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

Itzik Friedland
  • 247
  • 2
  • 7
0

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.

Rathan Kumar
  • 2,567
  • 2
  • 17
  • 24
  • You cannot register for this `Intent` action in the manifest. – CommonsWare Jan 12 '15 at 12:48
  • @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
  • So which is the best solution? – Paul Alexander Jan 12 '15 at 14:11
  • 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