is Battery Charging via Intent - Android android.os

Android examples for android.os:Battery

Description

is Battery Charging via Intent

Demo Code

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

public class Main{

    public static boolean isCharging(Context context) {
        Intent batteryStatus = getBatteryStatus(context);
        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS,
                -1);// w  w w .ja v  a2 s . c o  m
        return status == BatteryManager.BATTERY_STATUS_CHARGING
                || status == BatteryManager.BATTERY_STATUS_FULL;
    }
    private static Intent getBatteryStatus(Context context) {
        IntentFilter filter = new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED);
        return context.registerReceiver(null, filter);
    }

}

Related Tutorials