Example usage for android.content IntentFilter IntentFilter

List of usage examples for android.content IntentFilter IntentFilter

Introduction

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

Prototype

public IntentFilter(Parcel source) 

Source Link

Usage

From source file:Main.java

/**
 * Gets the battery info of device, and then writes it to a file.
 * //w  ww . j  ava 2 s.  com
 * @param context the context of application
 */
public static String getBatteryUsageInfo(Context context) {
    Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int rawlevel = batteryIntent.getIntExtra("level", -1);
    double scale = batteryIntent.getIntExtra("scale", -1);
    return String.format("Device has %s of %s battery left", rawlevel, scale);
}

From source file:Main.java

/**
 * @param context// w  ww .  j  a  va2 s. com
 * @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);
}

From source file:Main.java

public static void registerReceiver(Context context, BroadcastReceiver receive, String action) {
    IntentFilter filter = new IntentFilter(action);
    context.registerReceiver(receive, filter);
}

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

From source file:Main.java

public static boolean isCharging(Context context) {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, filter);
    int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean usbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB);
    boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
    boolean wirelessCharge = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        wirelessCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS);
    }/*from  ww w. j a va  2  s  . co m*/
    return (usbCharge || acCharge || wirelessCharge);
}

From source file:Main.java

public 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 boolean isPowerConnected(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (intent == null) {
        return true;
    }//from   w  w  w .j a  v  a2 s .co  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

private static IntentFilter createIntentFilter(String[] intentFilters) {
    IntentFilter filter = new IntentFilter(intentFilters[0]);

    for (int i = 0; i < intentFilters.length; i++) {
        if (i == 0) {
            continue;
        }/*from  w  w w.j a  v a2  s . co m*/
        filter.addAction(intentFilters[i]);
    }

    return filter;
}

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   ww w .  j  a  v  a2s .co  m*/
}

From source file:Main.java

/**
 * Returns true if the charger is currently connected
 * @param context/* w  ww . j  a  v a 2 s  .c o  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;
}