下面列出了android.os.BatteryManager#BATTERY_PLUGGED_WIRELESS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean isPoweredLocked(int plugTypeSet) {
// assume we are powered if battery state is unknown so
// the "stay on while plugged in" option will work.
if (mHealthInfo.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
return true;
}
if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mHealthInfo.chargerAcOnline) {
return true;
}
if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mHealthInfo.chargerUsbOnline) {
return true;
}
if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mHealthInfo.chargerWirelessOnline) {
return true;
}
return false;
}
private static String batteryPlugged(int status) {
String healthBat = BaseData.UNKNOWN_PARAM;
switch (status) {
case BatteryManager.BATTERY_PLUGGED_AC:
healthBat = "ac";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
healthBat = "usb";
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
healthBat = "wireless";
break;
default:
break;
}
return healthBat;
}
private String getBatteryStatus(Context context) {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
String battStatus;
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
boolean wirelessCharge = false;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
wirelessCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS;
if (usbCharge)
battStatus = context.getString(R.string.power_source_status_usb);
else if (acCharge)
battStatus = context.getString(R.string.power_source_status_ac);
else if (wirelessCharge)
battStatus = context.getString(R.string.power_source_status_wireless);
else battStatus = context.getString(R.string.power_disconnected);
return battStatus;
}
/**
* Gets charging source.
*
* @return the charging source
*/
@ChargingVia
public final int getChargingSource() {
Intent batteryStatus = getBatteryStatusIntent();
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
switch (chargePlug) {
case BatteryManager.BATTERY_PLUGGED_AC:
return ChargingVia.AC;
case BatteryManager.BATTERY_PLUGGED_USB:
return ChargingVia.USB;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
return ChargingVia.WIRELESS;
default:
return ChargingVia.UNKNOWN_SOURCE;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static BatteryStatus getBatteryStatus(Context context) {
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
if (intent == null) {
// should not happen
return BatteryStatus.DEFAULT;
}
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float) scale;
// 0 is on battery
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
boolean charging = 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);
return new BatteryStatus(charging, batteryPct);
}
/**
* Update the preference switch for {@link Settings.Global#STAY_ON_WHILE_PLUGGED_IN} setting.
*
* <p>
* If either one of the {@link BatteryManager#BATTERY_PLUGGED_AC},
* {@link BatteryManager#BATTERY_PLUGGED_USB}, {@link BatteryManager#BATTERY_PLUGGED_WIRELESS}
* values is set, we toggle the preference to true and update the setting value to
* {@link #BATTERY_PLUGGED_ANY}
* </p>
*/
private void updateStayOnWhilePluggedInPreference() {
if (!mStayOnWhilePluggedInSwitchPreference.isEnabled()) {
return;
}
boolean checked = false;
final int currentState = Settings.Global.getInt(getActivity().getContentResolver(),
Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0);
checked = (currentState &
(BatteryManager.BATTERY_PLUGGED_AC |
BatteryManager.BATTERY_PLUGGED_USB |
BatteryManager.BATTERY_PLUGGED_WIRELESS)) != 0;
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
checked ? BATTERY_PLUGGED_ANY : DONT_STAY_ON);
mStayOnWhilePluggedInSwitchPreference.setChecked(checked);
}
/**
* Conversion from plugged type int to String can be done through this method.
*
* @param plugged integer representing the plugged type.
* @return String representing the plugged type.
*/
private String getPlugType(int plugged) {
String plugType = UNKNOWN;
switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC:
plugType = AC;
break;
case BatteryManager.BATTERY_PLUGGED_USB:
plugType = USB;
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
plugType = WIRELESS;
break;
}
return plugType;
}
private boolean shouldWakeUpWhenPluggedOrUnpluggedLocked(
boolean wasPowered, int oldPlugType, boolean dockedOnWirelessCharger) {
// Don't wake when powered unless configured to do so.
if (!mWakeUpWhenPluggedOrUnpluggedConfig) {
return false;
}
// Don't wake when undocked from wireless charger.
// See WirelessChargerDetector for justification.
if (wasPowered && !mIsPowered
&& oldPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
return false;
}
// Don't wake when docked on wireless charger unless we are certain of it.
// See WirelessChargerDetector for justification.
if (!wasPowered && mIsPowered
&& mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS
&& !dockedOnWirelessCharger) {
return false;
}
// If already dreaming and becoming powered, then don't wake.
if (mIsPowered && mWakefulness == WAKEFULNESS_DREAMING) {
return false;
}
// Don't wake while theater mode is enabled.
if (mTheaterModeEnabled && !mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig) {
return false;
}
// On Always On Display, SystemUI shows the charging indicator
if (mAlwaysOnEnabled && mWakefulness == WAKEFULNESS_DOZING) {
return false;
}
// Otherwise wake up!
return true;
}
/**
* Updates the charging state and returns true if docking was detected.
*
* @param isPowered True if the device is powered.
* @param plugType The current plug type.
* @return True if the device is determined to have just been docked on a wireless
* charger, after suppressing spurious docking or undocking signals.
*/
public boolean update(boolean isPowered, int plugType) {
synchronized (mLock) {
final boolean wasPoweredWirelessly = mPoweredWirelessly;
if (isPowered && plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
// The device is receiving power from the wireless charger.
// Update the rest position asynchronously.
mPoweredWirelessly = true;
mMustUpdateRestPosition = true;
startDetectionLocked();
} else {
// The device may or may not be on the wireless charger depending on whether
// the unplug signal that we received was spurious.
mPoweredWirelessly = false;
if (mAtRest) {
if (plugType != 0 && plugType != BatteryManager.BATTERY_PLUGGED_WIRELESS) {
// The device was plugged into a new non-wireless power source.
// It's safe to assume that it is no longer on the wireless charger.
mMustUpdateRestPosition = false;
clearAtRestLocked();
} else {
// The device may still be on the wireless charger but we don't know.
// Check whether the device has remained at rest on the charger
// so that we will know to ignore the next wireless plug event
// if needed.
startDetectionLocked();
}
}
}
// Report that the device has been docked only if the device just started
// receiving power wirelessly and the device is not known to already be at rest
// on the wireless charger from earlier.
return mPoweredWirelessly && !wasPoweredWirelessly && !mAtRest;
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (batteryView != null) {
final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
final int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
final int batteryPct = Math.round(level / (float) scale * 100);
final int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
final boolean charged = chargePlug == BatteryManager.BATTERY_PLUGGED_AC || chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS || chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
batteryView.setLevel(batteryPct, charged);
if (lowBatteryAlert)
getWindow().setStatusBarColor((batteryPct < D.LOW_BATTERY_LEVEL && !charged) ? ContextCompat.getColor(context, R.color.battery_low) : D.DEFAULT_STATUS_BAR_COLOR);
}
}
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);
}
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;
}
public static String translateBatteryPlugged(int batteryPlugged) {
switch (batteryPlugged) {
case 0:
return "UNPLUGGED";
case BatteryManager.BATTERY_PLUGGED_AC:
return "AC";
case BatteryManager.BATTERY_PLUGGED_USB:
return "USB";
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
return "WIRELESS";
default:
return "UNKNOWN (" + batteryPlugged + ")";
}
}
/**
* @return true is device is plugged at this moment, false otherwise.
* @see #isPlugged(android.content.Context)
*/
@SuppressLint("InlinedApi")
public static boolean isPlugged(@Nullable Intent intent) {
if (intent == null) {
return false;
}
final int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC
|| plugged == BatteryManager.BATTERY_PLUGGED_USB
|| plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
@TargetApi(17)
private boolean isCharging_API17() {
Intent intent = mContext.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 ||
plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
/**
* @return true is device is plugged at this moment, false otherwise.
* @see #isPlugged(android.content.Context)
*/
@SuppressLint("InlinedApi")
public static boolean isPlugged(@Nullable Intent intent) {
if (intent == null) {
return false;
}
final int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC
|| plugged == BatteryManager.BATTERY_PLUGGED_USB
|| plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
static BatteryUsage getBatteryUsage(final Context context, Intent intent) {
BatteryUsage usage = new BatteryUsage();
BatteryDetails details = new BatteryDetails();
// Battery details
int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
String batteryTechnology = intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
String batteryHealth = "Unknown";
String batteryCharger = "unplugged";
String batteryStatus;
usage.timestamp = System.currentTimeMillis();
usage.id = String.valueOf(usage.timestamp).hashCode();
switch (health) {
case BatteryManager.BATTERY_HEALTH_DEAD:
batteryHealth = "Dead";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
batteryHealth = "Good";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
batteryHealth = "Over voltage";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
batteryHealth = "Overheat";
break;
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
batteryHealth = "Unknown";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
batteryHealth = "Unspecified failure";
break;
}
switch (status) {
case BatteryManager.BATTERY_STATUS_CHARGING:
batteryStatus = "Charging";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
batteryStatus = "Discharging";
break;
case BatteryManager.BATTERY_STATUS_FULL:
batteryStatus = "Full";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
batteryStatus = "Not charging";
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
batteryStatus = "Unknown";
break;
default:
batteryStatus = "Unknown";
}
switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC:
batteryCharger = "ac";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
batteryCharger = "usb";
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
batteryCharger = "wireless";
}
details.temperature =
((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)) / 10;
// current battery voltage in VOLTS
// (the unit of the returned value by BatteryManager is millivolts)
details.voltage =
((float) intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0)) / 1000;
details.charger = batteryCharger;
details.health = batteryHealth;
details.technology = batteryTechnology;
// Battery other values with API level limitations
details.capacity = Battery.getBatteryDesignCapacity(context);
details.chargeCounter = Battery.getBatteryChargeCounter(context);
details.currentAverage = Battery.getBatteryCurrentAverage(context);
details.currentNow = (int) Battery.getBatteryCurrentNow(context);
details.energyCounter = Battery.getBatteryEnergyCounter(context);
details.remainingCapacity = Battery.getBatteryRemainingCapacity(context);
usage.level = (float) sCurrentBatteryLevel;
usage.state = batteryStatus;
usage.screenOn = Screen.isOn(context);
usage.triggeredBy = intent.getAction();
usage.details = details;
return usage;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null) return;
boolean isCharging = false;
String batteryCharger = "";
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
isCharging = true;
final Intent mIntent = context.getApplicationContext()
.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
if (mIntent == null) return;
int chargePlug = mIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
boolean wirelessCharge = false;
if (Build.VERSION.SDK_INT >= 21) {
wirelessCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
if (acCharge) {
batteryCharger = "ac";
EventBus.getDefault().post(new PowerSourceEvent("ac"));
} else if (usbCharge) {
batteryCharger = "usb";
EventBus.getDefault().post(new PowerSourceEvent("usb"));
} else if (wirelessCharge) {
batteryCharger = "wireless";
EventBus.getDefault().post(new PowerSourceEvent("wireless"));
}
} else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
isCharging = false;
EventBus.getDefault().post(new PowerSourceEvent("unplugged"));
}
// Post to subscribers & update notification
int batteryRemaining =
(int) (Battery.getRemainingBatteryTime(context, isCharging, batteryCharger) / 60);
int batteryRemainingHours = batteryRemaining / 60;
int batteryRemainingMinutes = batteryRemaining % 60;
EventBus.getDefault().post(
new BatteryTimeEvent(batteryRemainingHours, batteryRemainingMinutes, isCharging)
);
// Notifier.remainingBatteryTimeAlert(
// context,
// batteryRemainingHours + "h " + batteryRemainingMinutes + "m", isCharging
// );
try {
// Save a new Battery Session to the mDatabase
GreenHubDb database = new GreenHubDb();
LogUtils.logI(TAG, "Getting new session");
database.saveSession(Inspector.getBatterySession(context, intent));
database.close();
} catch (IllegalStateException | RealmMigrationNeededException e) {
LogUtils.logE(TAG, "No session was created");
e.printStackTrace();
}
}
@SuppressLint("InlinedApi")
@Override
public void onReceive(Context context, Intent intent) {
sp = PreferenceManager.getDefaultSharedPreferences(context);
if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
intLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
switch (intent.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN)) {
case BatteryManager.BATTERY_STATUS_UNKNOWN: // 未知
C.BATTER_CHARING_STATUS = BatteryManager.BATTERY_STATUS_UNKNOWN;
break;
case BatteryManager.BATTERY_STATUS_CHARGING: // 充电
C.BATTER_CHARING_STATUS = BatteryManager.BATTERY_STATUS_CHARGING;
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING: // 未充电
C.BATTER_CHARING_STATUS = BatteryManager.BATTERY_STATUS_NOT_CHARGING;
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING: // 放电状态
C.BATTER_CHARING_STATUS = BatteryManager.BATTERY_STATUS_DISCHARGING;
break;
case BatteryManager.BATTERY_STATUS_FULL: // 充满电
C.BATTER_CHARING_STATUS = BatteryManager.BATTERY_STATUS_FULL;
break;
default:
break;
}
switch (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)) {
case BatteryManager.BATTERY_PLUGGED_USB:
C.BATTER_CHARING_TYPE = BatteryManager.BATTERY_PLUGGED_USB;
break;
case BatteryManager.BATTERY_PLUGGED_AC:
C.BATTER_CHARING_TYPE = BatteryManager.BATTERY_PLUGGED_AC;
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
C.BATTER_CHARING_TYPE = BatteryManager.BATTERY_PLUGGED_WIRELESS;
break;
default:
break;
}
if (sp.getBoolean("PowerSaving", true)) {
if (intLevel <= C.POWERSAVING) {
C.BATTER_STATUS = C.BATTER_LOWER_POWER;
}
}else{
C.BATTER_STATUS = C.BATTER_NORMAL_POWER;
}
}
/*
* MyLog.i(TAG,
* "MSSValue.BATTER_CHARING_TYPE "+MSSValue.BATTER_CHARING_TYPE);
* MyLog.i(TAG,
* "MSSValue.BATTER_CHARING_STATUS "+MSSValue.BATTER_CHARING_STATUS);
* MyLog.i(TAG, "health"+intent.getIntExtra("health",
* BatteryManager.BATTERY_HEALTH_UNKNOWN));
*/
MyLog.i(TAG, "BATTER_STATUS=" + C.BATTER_STATUS);
if (serivce != null)
serivce.runCharing();
}