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 int getBatteryLevel(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);
    return batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
}

From source file:Main.java

public static String getBatteryLevel(Context context) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent status = context.registerReceiver(null, intentFilter);
    int level = status.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    return String.valueOf(level);
}

From source file:Main.java

private static Intent getBatteryStatus(Context context) {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    return context.registerReceiver(null, filter);
}

From source file:Main.java

public static float getCurrentBattery(Context context) {
    Intent batteryInfoIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int status = batteryInfoIntent.getIntExtra("status", 0);
    int health = batteryInfoIntent.getIntExtra("health", 1);
    boolean present = batteryInfoIntent.getBooleanExtra("present", false);
    int level = batteryInfoIntent.getIntExtra("level", 0);
    int scale = batteryInfoIntent.getIntExtra("scale", 0);
    int plugged = batteryInfoIntent.getIntExtra("plugged", 0);
    int voltage = batteryInfoIntent.getIntExtra("voltage", 0);
    int temperature = batteryInfoIntent.getIntExtra("temperature", 0);
    String technology = batteryInfoIntent.getStringExtra("technology");
    return level / (float) scale;
}

From source file:Main.java

public static int getBatteryLevel(Context context) {

    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_BATTERY_CHANGED);
    Intent intent = context.registerReceiver(null, filter);
    if (intent == null) {
        return -1;
    }// ww  w  . j a  va2  s .  c  o  m
    return intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
}

From source file:Main.java

public static float getCurrentBattery(Context context) {
    Intent batteryInfoIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    //      int status = batteryInfoIntent.getIntExtra( "status" , 0 ); 
    //      int health = batteryInfoIntent.getIntExtra( "health" , 1 );  
    //      boolean present = batteryInfoIntent.getBooleanExtra( "present" , false );  
    int level = batteryInfoIntent.getIntExtra("level", 0);
    int scale = batteryInfoIntent.getIntExtra("scale", 0);
    //      int plugged = batteryInfoIntent.getIntExtra( "plugged" , 0 );  
    //      int voltage = batteryInfoIntent.getIntExtra( "voltage" , 0 );  
    //      int temperature = batteryInfoIntent.getIntExtra( "temperature" , 0 );
    //      String technology = batteryInfoIntent.getStringExtra( "technology" );  
    //      Log.i("Battery: ", "status:  " + status);
    //      Log.i("Battery: ", "health: " + health);
    //      Log.i("Battery: ", "present: " + present);
    //      Log.i("Battery: ", "level: " + level);
    //      Log.i("Battery: ", "scale: " + scale);
    //      Log.i("Battery: ", "plugged: " + plugged);
    //      Log.i("Battery: ", "voltage: " + voltage);
    //      Log.i("Battery: ", "temperature: " + temperature);
    //      Log.i("Battery: ", "technology: " + technology);
    return level / (float) scale;
}

From source file:Main.java

private static Boolean isCharging(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

    return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
}

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 but I added just in case.
    if (level == -1 || scale == -1) {
        return 50.0f;
    }// w w w .j  a  va  2s.  co  m

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

From source file:Main.java

public static int getBatteryPercent(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);
    assert batteryStatus != null;
    return batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
}

From source file:Main.java

public static float getChargePct(Context context) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, intentFilter);
    return getChargePct(batteryStatus);
}