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


Java BatteryManager.BATTERY_PLUGGED_AC屬性代碼示例

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


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

示例1: onReceive

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
        int remain = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        String remainString = Integer.toString(remain) + "%";
        currentStatus.setText(remainString);

        int plugIn = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
        switch (plugIn) {
            case 0:
                adapterStatus.setText("No Connection");
                break;

            case BatteryManager.BATTERY_PLUGGED_AC:
                adapterStatus.setText("Adapter Connected");
                break;

            case BatteryManager.BATTERY_PLUGGED_USB:
                adapterStatus.setText("USB Connected");
                break;
        }
    }
}
 
開發者ID:Jamjomjara,項目名稱:snu-artoon,代碼行數:25,代碼來源:MainActivity.java

示例2: 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

示例3: 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

示例4: 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

示例5: 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

示例6: setBatteryPct

private void setBatteryPct() {
    // Sets battery percentage
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);
    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    int batteryPct = Math.round(level / (float) scale * 100);
    int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean charging = chargePlug == BatteryManager.BATTERY_PLUGGED_USB || chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
    // Text
    ((TextView) statusBar.findViewById(R.id.battery_percentage)).setText(Integer.toString(batteryPct) + "%");
    // Icon
    int iconPct = 101;
    int iconId = -1;
    HashMap<Integer, Integer> iconMap = charging ? maxBatteryChargingToIcon : maxBatteryToIcon;
    for (int key : iconMap.keySet()) {
        if (key < iconPct && batteryPct <= key) {
            iconId = iconMap.get(key);
            iconPct = key;
        }
    }
    ((ImageView) statusBar.findViewById(R.id.battery_icon)).setImageResource(iconId);
}
 
開發者ID:AlstonLin,項目名稱:TheLearningLock,代碼行數:23,代碼來源:StatusBar.java

示例7: isCharging

private boolean isCharging(@NonNull Context context) {
    boolean charging = false;
    final Intent batteryIntent =
            context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (batteryIntent != null) {
        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;
    //        Intent intent = Config.getInstance().getContext().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;
}
 
開發者ID:Zeyad-37,項目名稱:UseCases,代碼行數:25,代碼來源:CloudStore.java

示例8: isCharging

public static boolean isCharging(Context context) {
    Bundle extras = getBatteryChangedExtras(context);
    int plugged = extras != null ? extras.getInt(BatteryManager.EXTRA_PLUGGED, 0) : 0;
    return plugged == BatteryManager.BATTERY_PLUGGED_AC
            || plugged == BatteryManager.BATTERY_PLUGGED_USB
            || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
            && plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS);
}
 
開發者ID:Doist,項目名稱:JobSchedulerCompat,代碼行數:8,代碼來源:DeviceUtils.java

示例9: 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

示例10: onBatteryChargingChanged

public static void onBatteryChargingChanged(Intent intent) {
    if (sInstance == null) {
        // We may be called by the framework intent-filter before being fully initialized. This
        // is not a problem, since our constructor will check for the state later on.
        return;
    }
    int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    // If we're not plugged, assume we're running on battery power.
    sInstance.mIsBatteryPower = chargePlug != BatteryManager.BATTERY_PLUGGED_USB
            && chargePlug != BatteryManager.BATTERY_PLUGGED_AC;
    nativeOnBatteryChargingChanged();
}
 
開發者ID:lizhangqu,項目名稱:chromium-net-for-android,代碼行數:12,代碼來源:PowerMonitor.java

示例11: isCharging

public boolean isCharging() {
    Intent intent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (intent == null) {
        throw new ScriptException("Cannot retrieve the battery state");
    }
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}
 
開發者ID:hyb1996,項目名稱:Auto.js,代碼行數:8,代碼來源:Device.java

示例12: isPluggedIn

private boolean isPluggedIn() {
  Intent batteryStatus = registerBatteryUpdates(MapboxTelemetry.applicationContext);
  int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, DEFAULT_BATTERY_LEVEL);
  final boolean pluggedIntoUSB = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
  final boolean pluggedIntoAC = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

  return pluggedIntoUSB || pluggedIntoAC;
}
 
開發者ID:mapbox,項目名稱:mapbox-events-android,代碼行數:8,代碼來源:MapEventFactory.java

示例13: battery

/**
 * Gets a callback each time there is a change in the battery status
 * @param callback
 *
 * @status TODO_EXAMPLE
 */
@ProtoMethod
public void battery(final ReturnInterface callback) {
    batteryReceiver = new BroadcastReceiver() {
        int scale = -1;
        int level = -1;
        int voltage = -1;
        int temp = -1;
        boolean isConnected = false;
        private int status;
        private final boolean alreadyKilled = false;

        @Override
        public void onReceive(Context context, Intent intent) {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
            // isCharging =
            // intent.getBooleanExtra(BatteryManager.EXTRA_PLUGGED, false);
            // status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            status = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

            if (status == BatteryManager.BATTERY_PLUGGED_AC) {
                isConnected = true;
            } else if (status == BatteryManager.BATTERY_PLUGGED_USB) {
                isConnected = true;
            } else {
                isConnected = false;
            }

            ReturnObject o = new ReturnObject();

            o.put("level", level);
            o.put("temperature", temp);
            o.put("connected", isConnected);
            o.put("scale", scale);
            o.put("temperature", temp);
            o.put("voltage", voltage);

            callback.event(o);
        }
    };

    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    getContext().registerReceiver(batteryReceiver, filter);
}
 
開發者ID:victordiaz,項目名稱:phonk,代碼行數:52,代碼來源:PDevice.java

示例14: isConnectedToCharger

public static boolean isConnectedToCharger(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (intent != null) {
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
    } else return false;
}
 
開發者ID:theblixguy,項目名稱:ForceDoze,代碼行數:7,代碼來源:Utils.java

示例15: onReceive

@Override
public void onReceive(Context context, Intent intent) {
    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean charging = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
    currentBattery = level;
    Utils.logDebug(MAIN_SERVICE_LOG_TAG, "Battery level " + level);

    if (batteryTV != null)
        batteryTV.setText(String.valueOf(level) + "%");
    if (batteryIV != null) {
        int res;
        if (charging)
            res = R.drawable.ic_battery_charging;
        else {
            if (level > 90)
                res = R.drawable.ic_battery_full;
            else if (level > 70)
                res = R.drawable.ic_battery_90;
            else if (level > 50)
                res = R.drawable.ic_battery_60;
            else if (level > 30)
                res = R.drawable.ic_battery_30;
            else if (level > 20)
                res = R.drawable.ic_battery_20;
            else if (level > 0)
                res = R.drawable.ic_battery_alert;
            else
                res = R.drawable.ic_battery_unknown;
        }
        batteryIV.setImageResource(res);
    }
}
 
開發者ID:rosenpin,項目名稱:AlwaysOnDisplayAmoled,代碼行數:33,代碼來源:BatteryReceiver.java


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