battery Power Is Low - Android Hardware

Android examples for Hardware:Battery

Description

battery Power Is Low

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

public class Main {
    public static boolean batteryPowerIsLow(Context context) {

        if (context == null)
            throw new NullPointerException("context cannot be null");

        final int PowerSaveThreshold = 20;

        int batteryLevel = getCurrentBatteryChargePercentage(context);

        return (batteryLevel <= PowerSaveThreshold) ? true : false;
    }/*w w  w  .  jav a2  s . c om*/

    public static int getCurrentBatteryChargePercentage(Context context) {
        IntentFilter iFilter = new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, iFilter);

        final int DefaultValue = 0;

        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL,
                DefaultValue);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE,
                DefaultValue);

        int batteryPct = calculateBatteryPercentage(level, scale);

        return batteryPct;
    }

    private static int calculateBatteryPercentage(int level, int scale) {
        return Math.round((level / (float) scale) * 100);
    }
}

Related Tutorials