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 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;
    }//from w  w  w.j  a v a2  s  .co m

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

From source file:Main.java

public static int getIntExtra(Intent intent, String name, int defaultValue) {
    if (!hasIntent(intent) || !hasExtra(intent, name))
        return defaultValue;
    return intent.getIntExtra(name, defaultValue);
}

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;
    }//from ww  w .  j  a va  2s .  c om
    return ((float) level / (float) scale) * 100.0f;
}

From source file:Main.java

/**
 * @return true is device is plugged at this moment, false otherwise.
 * @see #isPlugged(android.content.Context)
 *///www . j a va 2s .  c  o m
@SuppressLint("InlinedApi")
public static boolean isPlugged(@Nullable Intent intent) {
    if (intent == null) {
        return false;
    }

    final 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

/**
 * Gets the battery info of device, and then writes it to a file.
 * /*from  w  w w  .ja  v a 2s  . 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

public static void setAlarmTime(Context context, long timeInMillis, Intent intent) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent sender = PendingIntent.getBroadcast(context, intent.getIntExtra("id", 0), intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    int interval = (int) intent.getLongExtra("intervalMillis", 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setWindow(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender);
    }//from w w w  . ja  v  a  2  s  .c  o m
}

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  ww  w . j  a  va  2s. c om
    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:gxu.software_engineering.market.android.util.ServiceHelper.java

public static void pre(Intent intent) {
    int target = intent.getIntExtra(C.TARGET_ENTITY, -1);
    long uid = Long.MIN_VALUE;
    long cid = Long.MIN_VALUE;
    switch (target) {
    case CATEGORIES:
        intent.putExtra(C.HTTP_URI, C.DOMAIN + "/categories");
        break;/*from   www . ja v  a2  s.  c  o  m*/
    case LASTEST_USERS:
        intent.putExtra(C.HTTP_URI, C.DOMAIN + "/users?type=1&count=50");
        break;
    case LASTEST_ITEMS:
        intent.putExtra(C.HTTP_URI, C.DOMAIN + "/items?type=1&count=50");
        break;
    case HOTTEST_ITEMS:
        intent.putExtra(C.HTTP_URI, C.DOMAIN + "/items?type=6&count=20");
        break;
    case USER_ITEMS:
        uid = intent.getLongExtra(C.UID, -1);
        intent.putExtra(C.HTTP_URI, C.DOMAIN
                + String.format("/items?type=4&count=%d&uid=%d&deal=0&last_id=0", C.DEFAULT_LIST_SIZE, uid));
        break;
    case CATEGORY_ITEMS:
        cid = intent.getLongExtra(C.CID, -1);
        intent.putExtra(C.HTTP_URI,
                C.DOMAIN + String.format("/items?type=5&count=%d&cid=%d&last_id=0", C.DEFAULT_LIST_SIZE, cid));
        break;
    case USER_CLOSED_ITEMS:
        uid = intent.getLongExtra(C.UID, -1);
        intent.putExtra(C.HTTP_URI,
                C.DOMAIN + String.format("/items?type=7&count=%d&uid=%d&last_id=0", C.DEFAULT_LIST_SIZE, uid));
        break;
    case USER_DEAL_ITEMS:
        uid = intent.getLongExtra(C.UID, uid);
        intent.putExtra(C.HTTP_URI,
                C.DOMAIN + String.format("/items?type=4&count=%d&uid=%d&deal=1", C.DEFAULT_LIST_SIZE, uid));
        break;
    default:
        throw new IllegalArgumentException("sorry, 404 for the target[" + target + "]");
    }
}

From source file:com.andremion.heroes.ui.section.view.SectionActivity.java

public static int getType(int resultCode, Intent data) {
    if (resultCode == RESULT_OK && data != null && data.hasExtra(EXTRA_TYPE)) {
        return data.getIntExtra(EXTRA_TYPE, EXTRA_NOT_FOUND);
    }//from  w ww  . ja  v a  2 s.c  om
    return EXTRA_NOT_FOUND;
}