Example usage for android.content Intent ACTION_BATTERY_CHANGED

List of usage examples for android.content Intent ACTION_BATTERY_CHANGED

Introduction

In this page you can find the example usage for android.content Intent ACTION_BATTERY_CHANGED.

Prototype

String ACTION_BATTERY_CHANGED

To view the source code for android.content Intent ACTION_BATTERY_CHANGED.

Click Source Link

Document

Broadcast Action: This is a sticky broadcast containing the charging state, level, and other information about the battery.

Usage

From source file:Main.java

public static boolean isPowerConnected(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (intent == null) {
        return true;
    }/*  w w  w . j ava2  s .c o  m*/
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB
            || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}

From source file:Main.java

public static void dumpBatteryStats(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);

    Bundle bundle = batteryStatus.getExtras();
    for (String key : bundle.keySet()) {
        Object value = bundle.get(key);
        Log.d(TAG, String.format("Battery,%s=%s (%s)", key, value.toString(), value.getClass().getName()));
    }//from   w  w w.ja  v a2  s.co  m
}

From source file:Main.java

/**
 * Returns true if the charger is currently connected
 * @param context/*from   w  w w  .j av  a 2  s . c om*/
 * @return true if the charger is connected
 */
public static boolean isConnected(Context context) {
    // make a synchronous call
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}

From source file:Main.java

public static boolean isPowerConnected(Context context) {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = ((status == BatteryManager.BATTERY_STATUS_CHARGING)
            || (status == BatteryManager.BATTERY_STATUS_FULL));

    return isCharging;
}

From source file:Main.java

public static float getBatteryLevel(Context context) {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    // Error checking that probably isn't needed, just in case.
    if (level == -1 || scale == -1) {
        return 0.50f;
    }/*  ww w . j  a v a  2 s  . c om*/

    return ((float) level / (float) scale);
}

From source file:Main.java

/**
 * @return true is device is plugged at this moment, false otherwise.
 * @see #isPlugged(android.content.Intent)
 *//*from   w  ww.  j a v  a  2 s .  c o  m*/
public static boolean isPlugged(@NonNull Context context) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    return isPlugged(context.getApplicationContext().registerReceiver(null, intentFilter));
}

From source file:com.mendhak.gpslogger.common.Systems.java

public static int getBatteryLevel(Context context) {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent != null ? batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : 0;
    int scale = batteryIntent != null ? batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : 0;

    if (level == -1 || scale == -1) {
        return 50;
    }//from w ww .j a  v a 2s .com

    return (int) (((float) level / (float) scale) * 100.0f);
}

From source file:com.owncloud.android.utils.ConnectivityUtils.java

public static boolean isCharging(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);

    int status = 0;
    if (batteryStatus != null) {
        status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    }// w w w  .  j  av  a  2  s. co m
    return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
}

From source file:org.mozilla.mozstumbler.service.scanners.ScanManager.java

public boolean isBatteryLow() {
    Intent intent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (intent == null)
        return false;

    int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING);
    int level = Math.round(rawLevel * scale / 100.0f);

    final int kMinBatteryPct = 15;
    return !isCharging && level < kMinBatteryPct;
}

From source file:com.adam.aslfms.util.Util.java

/**
 * Returns whether the phone is running on battery or if it is connected to
 * a charger.//from  w ww  . jav a  2  s.  c om
 *
 * @param ctx context to get access to battery-checking methods
 * @return an enum indicating what the power source is
 * @see PowerOptions
 */
public static PowerOptions checkPower(Context ctx) {

    IntentFilter battFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent intent = ctx.registerReceiver(null, battFilter);

    if (intent != null) {
        int plugged = intent.getIntExtra("plugged", -1);
        if (plugged == 0) { // == 0 means on battery
            return PowerOptions.BATTERY;
        } else {
            return PowerOptions.PLUGGED_IN;
        }
    }

    Log.d(TAG, "Failed to get intent, assuming battery");
    return PowerOptions.BATTERY;
}