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

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

From source file:Main.java

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

From source file:Main.java

public static BroadcastReceiver registerForBroadcast(String filter, Context context,
        BroadcastReceiver receiver) {/*from  www  . j ava  2  s  .com*/
    IntentFilter intentFilter = new IntentFilter(filter);
    context.registerReceiver(receiver, intentFilter);
    return receiver;
}

From source file:Main.java

private static Intent getChargingIntent(Context paramContext) {
    if (paramContext == null) {
        return null;
    }// w  w w. j a v  a  2  s.c o m
    return paramContext.registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
}

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

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;
    }// w ww  .  j  a  v  a2s.com

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

From source file:Main.java

/**
 * return between 0 and 1.//  ww  w  .j av  a2  s. c o  m
 */
public static float level(final Context context) {
    final IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    final Intent batteryStatus = context.registerReceiver(null, ifilter);
    final int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    final int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    return level / (float) scale;
}

From source file:Main.java

public static float getBatteryLevel(Context applicationContext) {
    Intent batteryIntent = applicationContext.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  ww .  j  a v a 2  s . c om*/
    return ((float) level / (float) scale) * 100.0f;
}

From source file:Main.java

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

From source file:Main.java

public static boolean isMyLauncherDefault(Context context) {
    final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
    filter.addCategory(Intent.CATEGORY_HOME);

    List<IntentFilter> filters = new ArrayList<IntentFilter>();
    filters.add(filter);//from ww  w.ja  v a  2  s.  c  om

    final String myPackageName = context.getPackageName();
    List<ComponentName> activities = new ArrayList<>();
    PackageManager packageManager = (PackageManager) context.getPackageManager();

    // You can use name of your package here as third argument
    packageManager.getPreferredActivities(filters, activities, null);

    if (activities.size() == 0) //no default
        return true;

    for (ComponentName activity : activities) {
        if (myPackageName.equals(activity.getPackageName())) {
            return true;
        }
    }
    return false;
}