Code example for Intent

Methods: getActiongetBooleanExtragetIntExtra

0
        } 
    } 
 
    @Override 
    public void onReceive(Context context, Intent intent) {
        if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
            Log.e(LOGTAG, "Got an unexpected intent!");
            return; 
        } 
 
        boolean previousCharging = isCharging();
        double previousLevel = getLevel();
 
        // NOTE: it might not be common (in 2012) but technically, Android can run 
        // on a device that has no battery so we want to make sure it's not the case 
        // before bothering checking for battery state. 
        // However, the Galaxy Nexus phone advertizes itself as battery-less which 
        // force us to special-case the logic. 
        // See the Google bug: https://code.google.com/p/android/issues/detail?id=22035 
        if (intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false) ||
                Build.MODEL.equals("Galaxy Nexus")) {
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            if (plugged == -1) {
                sCharging = kDefaultCharging;
                Log.e(LOGTAG, "Failed to get the plugged status!");
            } else { 
                // Likely, if plugged > 0, it's likely plugged and charging but the doc 
                // isn't clear about that. 
                sCharging = plugged != 0;
            } 
 
            if (sCharging != previousCharging) {
                sRemainingTime = kUnknownRemainingTime;
                // The new remaining time is going to take some time to show up but 
                // it's the best way to show a not too wrong value. 
                sLastLevelChange = 0;
            } 
 
            // We need two doubles because sLevel is a double. 
            double current =  (double)intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            double max = (double)intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            if (current == -1 || max == -1) {
                Log.e(LOGTAG, "Failed to get battery level!");
                sLevel = kDefaultLevel;
            } else { 
                sLevel = current / max;