get Charging Mode - Android android.os

Android examples for android.os:BatteryManager

Description

get Charging Mode

Demo Code

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

public class Main {

  /**/*  w  w w. j  ava  2 s.  c  om*/
   *
   * @param context
   * @return BatteryManager.BATTERY_PLUGGED_USB,
   *         BatteryManager.BATTERY_PLUGGED_AC,
   *         BatteryManager.BATTERY_PLUGGED_WIRELESS
   */
  public static int getChargingMode(Context context) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, intentFilter);
    return getChargingMode(batteryStatus);
  }

  public static int getChargingMode(Intent batteryStatus) {
    return batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
  }

}

Related Tutorials