当前位置: 首页>>代码示例>>Java>>正文


Java BatteryManager.BATTERY_PLUGGED_USB属性代码示例

本文整理汇总了Java中android.os.BatteryManager.BATTERY_PLUGGED_USB属性的典型用法代码示例。如果您正苦于以下问题:Java BatteryManager.BATTERY_PLUGGED_USB属性的具体用法?Java BatteryManager.BATTERY_PLUGGED_USB怎么用?Java BatteryManager.BATTERY_PLUGGED_USB使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在android.os.BatteryManager的用法示例。


在下文中一共展示了BatteryManager.BATTERY_PLUGGED_USB属性的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_USB属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。