本文整理汇总了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;
}
}
}
示例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);
}
示例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();
}
}
示例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;
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
示例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;
}
}
示例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();
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
}