當前位置: 首頁>>代碼示例>>Java>>正文


Java BatteryManager.BATTERY_STATUS_CHARGING屬性代碼示例

本文整理匯總了Java中android.os.BatteryManager.BATTERY_STATUS_CHARGING屬性的典型用法代碼示例。如果您正苦於以下問題:Java BatteryManager.BATTERY_STATUS_CHARGING屬性的具體用法?Java BatteryManager.BATTERY_STATUS_CHARGING怎麽用?Java BatteryManager.BATTERY_STATUS_CHARGING使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在android.os.BatteryManager的用法示例。


在下文中一共展示了BatteryManager.BATTERY_STATUS_CHARGING屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: onReceive

@Override
public void onReceive(Context context, Intent intent) {
    // 接收電量變化信息
    if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
        int level = intent.getIntExtra("level", 0);
        int scale = intent.getIntExtra("scale", 100);
        // 電量百分比
        int curPower = level * 100 / scale;
        int status = intent.getIntExtra("status", BatteryManager.BATTERY_HEALTH_UNKNOWN);
        // SecondaryProgress 用來展示低電量,Progress 用來展示正常電量
        if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
            mPbBatteryLevel.setSecondaryProgress(0);
            mPbBatteryLevel.setProgress(curPower);
            mPbBatteryLevel.setBackgroundResource(R.mipmap.ic_battery_charging);
        } else if (curPower < BATTERY_LOW_LEVEL) {
            mPbBatteryLevel.setProgress(0);
            mPbBatteryLevel.setSecondaryProgress(curPower);
            mPbBatteryLevel.setBackgroundResource(R.mipmap.ic_battery_red);
        } else {
            mPbBatteryLevel.setSecondaryProgress(0);
            mPbBatteryLevel.setProgress(curPower);
            mPbBatteryLevel.setBackgroundResource(R.mipmap.ic_battery);
        }
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:25,代碼來源:IjkPlayerView.java

示例2: handleBattery

private void handleBattery(Intent intent){
    int level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
    int scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
    int levelPercent = (int)(((float)level / scale) * 100);
    boolean charging = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
            BatteryManager.BATTERY_STATUS_UNKNOWN) == BatteryManager.BATTERY_STATUS_CHARGING;
    if(levelPercent < BATTERY_LOW_LIMIT && !charging)
        AppModel.sDevBatteryLow = true;
    else
        AppModel.sDevBatteryLow = false;
    LogUtils.i("battery:" + level +"%, BatteryLow=" + AppModel.sDevBatteryLow);
}
 
開發者ID:XndroidDev,項目名稱:Xndroid,代碼行數:12,代碼來源:XndroidReceiver.java

示例3: getBatteryReading

public  void getBatteryReading(SensorDataListener listener){

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, intentFilter);

        int batteryStatusIntExtra = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = batteryStatusIntExtra == BatteryManager.BATTERY_STATUS_CHARGING ||
                batteryStatusIntExtra == BatteryManager.BATTERY_STATUS_FULL;

        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        float batteryPct = level / (float)scale;

        Log.i(TAG, "AC connected: "+acCharge);
        Log.i(TAG, "USB connected: "+usbCharge);
        Log.i(TAG, "Battery charging: "+ isCharging);
        Log.i(TAG, "Battery Level: "+ batteryPct);

        ArrayMap<String, String> map = new ArrayMap<>(3);
        map.put(SENSOR, "Battery");
        map.put(VALUE, Integer.toString(level));
        map.put(UNIT, BATTERYSENSOR_UNIT);
        map.put("charging", Boolean.toString(isCharging));
        map.put("acPlugged", Boolean.toString(acCharge));
        map.put("usbPlugged", Boolean.toString(usbCharge));
        listener.sensorData(map);
    }
 
開發者ID:ray0711,項目名稱:homeDash,代碼行數:32,代碼來源:SensorReader.java

示例4: onReceive

@Override
public void onReceive(Context context, Intent intent) {
    int alertValue = PreferenceManager.getDefaultSharedPreferences(context).getInt(MainActivity.KEY_BATTERY_PERCENT, 20);

    int status = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    float level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    float scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_AC;
    float value = (level * 100f) / scale;

    if (!isCharging && (int) value == alertValue) {
        ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(3000);
        Toast.makeText(context, String.format(context.getString(R.string.msg_low_battery), (int) value), Toast.LENGTH_LONG).show();
    }
}
 
開發者ID:TheAndroidMaster,項目名稱:BatteryAlert,代碼行數:17,代碼來源:BatteryReceiver.java

示例5: isPhonePluggedIn

public static boolean isPhonePluggedIn(Context context){
    boolean charging = false;

    final Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean batteryCharge = status==BatteryManager.BATTERY_STATUS_CHARGING;

    int chargePlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
    boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

    if (batteryCharge) charging=true;
    if (usbCharge) charging=true;
    if (acCharge) charging=true;

    return charging;
}
 
開發者ID:hanuor,項目名稱:sapphire,代碼行數:17,代碼來源:UploadDocs.java

示例6: onReceive

@Override
public void onReceive(Context context, Intent intent) {
    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, INVALID);
    int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, INVALID);
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, INVALID);
    int source = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, INVALID);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
    String type = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
    double temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, INVALID) / 10.0;
    int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, INVALID);

    mBatteryState.setLevel(level);
    mBatteryState.setHealth(health);
    mBatteryState.setCharging(isCharging);
    mBatteryState.setStatus(status);
    mBatteryState.setSource(source);
    mBatteryState.setCapacity(getCapacity());
    mBatteryState.setType(type);
    mBatteryState.setTemperature(temperature);
    mBatteryState.setVoltage(voltage);

    mListener.onBatteryStateChanged(mBatteryState);
}
 
開發者ID:Smart-Studio,項目名稱:device-info,代碼行數:23,代碼來源:BatteryStateProviderImpl.java

示例7: addBatteryStateTo

private void addBatteryStateTo(Collection<IoTDataField> dataFields) {
	IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
	Intent intent = mContext.registerReceiver(null, intentFilter);

	int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
	boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING;
	IoTDataField.BooleanField chargingField = new IoTDataField.BooleanField("charging", isCharging);
	dataFields.add(chargingField);

	int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
	boolean isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
	IoTDataField.BooleanField pluggedField = new IoTDataField.BooleanField("plugged", isPlugged);
	dataFields.add(pluggedField);

	int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
	int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
	float batteryPercent = level / (float) scale;
	// Scale to percents.
	batteryPercent *= 100;
	IoTDataField.IntField batteryPercentField = new IoTDataField.IntField("batteryPrecent", (int) batteryPercent);
	dataFields.add(batteryPercentField);
}
 
開發者ID:Flowdalic,項目名稱:android-xmpp-iot-demo,代碼行數:22,代碼來源:XmppIotThing.java

示例8: getBatteryStatusText

/** Get readable battery status */
private String getBatteryStatusText(int status) {
    String statusString = getString(R.string.unknown);
    switch (status) {
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            statusString = getString(R.string.unknown);
            break;
        case BatteryManager.BATTERY_STATUS_CHARGING:
            statusString = getString(R.string.charging);
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            statusString = getString(R.string.discharging);
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            statusString = getString(R.string.not_charging);
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            statusString = getString(R.string.full);
            break;
    }

    return statusString;
}
 
開發者ID:MotorolaMobilityLLC,項目名稱:mdkbattery,代碼行數:23,代碼來源:MainActivity.java

示例9: onReceive

@Override
public void onReceive(BatteryIconData icon, Intent intent) {
    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);

    int iconLevel = (int) (((float) level / scale) * 6) + 1;

    if (status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL)
        iconLevel += 7;

    icon.onDrawableUpdate(iconLevel);

    if (icon.hasText())
        icon.onTextUpdate(String.valueOf((int) (((double) level / scale) * 100)) + "%");
}
 
開發者ID:TheAndroidMaster,項目名稱:Status,代碼行數:16,代碼來源:BatteryIconData.java

示例10: isCharging

/**
 * Method to check if the device is charging.
 *
 * @return Returns true if device is either charging or full, false otherwise
 */
private boolean isCharging() {
    try {
        Intent batteryIntent = mContext.getApplicationContext().registerReceiver(null,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        if (batteryIntent != null) {
            int batteryPowerStatus = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);

            switch (batteryPowerStatus) {
                case BatteryManager.BATTERY_STATUS_CHARGING:
                case BatteryManager.BATTERY_STATUS_FULL:
                    return true;

                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                default:
                    return false;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception occurred while isCharging: " + e);
    }

    return false;
}
 
開發者ID:hypertrack,項目名稱:smart-scheduler-android,代碼行數:30,代碼來源:SmartScheduler.java

示例11: translateBatteryStatus

public static String translateBatteryStatus(int batteryStatus) {
    switch (batteryStatus) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
            return "CHARGING";
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            return "DISCHARGING";
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            return "NOT CHARGING";
        case BatteryManager.BATTERY_STATUS_FULL:
            return "FULL";
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            return "STATUS UNKNOWN";
        default:
            return "UNKNOWN (" + batteryStatus + ")";
    }
}
 
開發者ID:patrickfav,項目名稱:under-the-hood,代碼行數:16,代碼來源:TypeTranslators.java

示例12: onReceive

@Override
public void onReceive(Context context, Intent intent) {
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
            BatteryManager.BATTERY_STATUS_UNKNOWN);
    if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
        // 充電中
        mBattery.setImageResource(R.drawable.battery_charging);
    } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
        // 充電完成
        mBattery.setImageResource(R.drawable.battery_full);
    } else {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
        int percentage = (int) (((float) level / scale) * 100);
        if (percentage <= 10) {
            mBattery.setImageResource(R.drawable.battery_10);
        } else if (percentage <= 20) {
            mBattery.setImageResource(R.drawable.battery_20);
        } else if (percentage <= 50) {
            mBattery.setImageResource(R.drawable.battery_50);
        } else if (percentage <= 80) {
            mBattery.setImageResource(R.drawable.battery_80);
        } else if (percentage <= 100) {
            mBattery.setImageResource(R.drawable.battery_100);
        }
    }
}
 
開發者ID:crazysunj,項目名稱:CrazyDaily,代碼行數:27,代碼來源:NeihanVideoPlayerController.java

示例13: setBat

private void setBat(Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

        mIsCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;

//        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        mBatteryPercent = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

        Tile battery = getQsTile();

        int resId;

        if (mBatteryPercent >= 95) {
             resId = mIsCharging ? R.drawable.ic_battery_charging_full : R.drawable.ic_battery;
        } else if (mBatteryPercent >= 85) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_90 : R.drawable.ic_battery_90;
        } else if (mBatteryPercent >= 70) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_80 : R.drawable.ic_battery_80;
        } else if (mBatteryPercent >= 55) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_60 : R.drawable.ic_battery_60;
        } else if (mBatteryPercent >= 40) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_50 : R.drawable.ic_battery_50;
        } else if (mBatteryPercent >= 25) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_30 : R.drawable.ic_battery_30;
        } else if (mBatteryPercent >= 15) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_20 : R.drawable.ic_battery_20;
        } else {
            resId = mIsCharging ? R.drawable.ic_battery_charging_20 : R.drawable.ic_battery_alert;
        }

        battery.setIcon(Icon.createWithResource(this, resId));
        battery.setLabel(String.valueOf(mBatteryPercent).concat("%"));
        battery.updateTile();
    }
 
開發者ID:zacharee,項目名稱:SystemUITuner2,代碼行數:36,代碼來源:BatteryTileService.java

示例14: onReceive

public void onReceive(Context context, Intent intent) {
    int pct=
            100 * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 1)
                    / intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);

    barra_carga.setProgress(pct);
    nivel_carga.setText(String.valueOf(pct) + "%");


    switch (intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
            status_carga.setText("carregando");
            break;

        case BatteryManager.BATTERY_STATUS_FULL:
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

            if (plugged == BatteryManager.BATTERY_PLUGGED_AC
                    || plugged == BatteryManager.BATTERY_PLUGGED_USB) {
                status_carga.setText("bateria cheia, plugada");
            }
            else {
                status_carga.setText("bateria cheia, mas descarregando");
            }
            break;

        default:
            status_carga.setText("bateria descarregando");
            break;
    }
}
 
開發者ID:if710,項目名稱:2017.2-codigo,代碼行數:31,代碼來源:BateriaActivity.java

示例15: onReceive

@Override
public void onReceive(Context context, Intent intent) {
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean charging = status == BatteryManager.BATTERY_STATUS_CHARGING;

    if (charging || !mScreenOn) {
        mLevel = 0;
        mTime = 0;
    } else {
        mCalculating = true;

        long time = System.nanoTime();
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);

        if (mLevel != 0 && mLevel > level && mTime != 0 && mTime < time && mLevel - level > 0) {
            long seconds = TimeUnit.SECONDS.convert((time - mTime) / (mLevel - level),
                    TimeUnit.NANOSECONDS);
            if (seconds >= 100) {
                mTimes.add(seconds);

                if (mTimes.size() % 15 == 0) {
                    postCreate(mTimes.toArray(new Long[mTimes.size()]));
                    if (mTimes.size() >= 100) {
                        mTimes.clear();
                    }
                }
            }
        }
        mLevel = level;
        mTime = time;
    }

    mCalculating = false;
}
 
開發者ID:morogoku,項目名稱:MTweaks-KernelAdiutorMOD,代碼行數:34,代碼來源:Monitor.java


注:本文中的android.os.BatteryManager.BATTERY_STATUS_CHARGING屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。