Example usage for android.content Intent getIntExtra

List of usage examples for android.content Intent getIntExtra

Introduction

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

Prototype

public int getIntExtra(String name, int defaultValue) 

Source Link

Document

Retrieve extended data from the intent.

Usage

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 getBatteryLevel(Context context) {
    Intent batteryStatus = getBatteryStatus(context);
    if (batteryStatus != null) {
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        return (level / (float) scale);
    } else {//  ww w . j a  v a 2  s  .  co  m
        return 0.5f;
    }
}

From source file:Main.java

public static float level(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;

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

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;
    }//from   ww  w. j  a v  a 2s.  c  o  m

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

From source file:Main.java

private 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;
    }/*from  w  w w  .  j  a v  a  2 s  .  c om*/

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

From source file:Main.java

/**
 * Returns true if the charger is currently connected
 * @param context//  w  ww . j a  v  a  2  s. co m
 * @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 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;
    }//  w w w  . j  a v  a 2 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");
    return level / (float) scale;
}

From source file:Main.java

/**
 * @see #isPlugged(android.content.Context)
 * @see #isPlugged(android.content.Intent)
 *///from   ww w  .  ja  v a 2s . c  om
@SuppressLint("InlinedApi")
public static int getBatteryLevel(@Nullable Intent intent) {
    if (intent == null) {
        return 100;
    }

    final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    final int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    return level * 100 / scale;
}

From source file:Main.java

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;
}