Java Code Examples for android.os.BatteryManager#BATTERY_STATUS_UNKNOWN
The following examples show how to use
android.os.BatteryManager#BATTERY_STATUS_UNKNOWN .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Util.java From android-notification-log with MIT License | 7 votes |
public static String getBatteryStatus(Context context) { if(Build.VERSION.SDK_INT < 26) { return "not supported"; } try { BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE); if(bm != null) { int status = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS); switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: return "charging"; case BatteryManager.BATTERY_STATUS_DISCHARGING: return "discharging"; case BatteryManager.BATTERY_STATUS_FULL: return "full"; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: return "not charging"; case BatteryManager.BATTERY_STATUS_UNKNOWN: return "unknown"; default: return ""+status; } } } catch (Exception e) { if(Const.DEBUG) e.printStackTrace(); } return "undefined"; }
Example 2
Source File: BatteryService.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
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; }
Example 3
Source File: BatteryInfo.java From MobileInfo with Apache License 2.0 | 6 votes |
private static String batteryStatus(int status) { String healthBat = BaseData.UNKNOWN_PARAM; switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: healthBat = "charging"; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: healthBat = "disCharging"; break; case BatteryManager.BATTERY_STATUS_FULL: healthBat = "full"; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: healthBat = "notCharging"; break; case BatteryManager.BATTERY_STATUS_UNKNOWN: healthBat = "unknown"; break; default: break; } return healthBat; }
Example 4
Source File: BatteryChangeReceiver.java From CacheEmulatorChecker with Apache License 2.0 | 6 votes |
@Override public void onReceive(Context context, Intent intent) { int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0); mCurrentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: // 电池满不作为参考,看做充电中 case BatteryManager.BATTERY_STATUS_FULL: case BatteryManager.BATTERY_STATUS_UNKNOWN: mIsCharging = true; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: case BatteryManager.BATTERY_STATUS_DISCHARGING: mIsCharging = false; break; } // Toast.makeText(context, " " + mCurrentLevel + " mIsCharging "+ mIsCharging, Toast.LENGTH_SHORT).show(); }
Example 5
Source File: TypeTranslators.java From under-the-hood with Apache License 2.0 | 6 votes |
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 + ")"; } }
Example 6
Source File: SKBatteryData.java From SensingKit-Android with GNU Lesser General Public License v3.0 | 6 votes |
private static String getBatteryStatusString(int status) { switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: return "charging"; case BatteryManager.BATTERY_STATUS_DISCHARGING: return "discharging"; case BatteryManager.BATTERY_STATUS_FULL: return "full"; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: return "not Charging"; case BatteryManager.BATTERY_STATUS_UNKNOWN: return "unknown"; default: return "unsupported"; } }
Example 7
Source File: BatteryService.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
private boolean shouldSendBatteryLowLocked() { final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE; final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE; /* The ACTION_BATTERY_LOW broadcast is sent in these situations: * - is just un-plugged (previously was plugged) and battery level is * less than or equal to WARNING, or * - is not plugged and battery level falls to WARNING boundary * (becomes <= mLowBatteryWarningLevel). */ return !plugged && mHealthInfo.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN && mHealthInfo.batteryLevel <= mLowBatteryWarningLevel && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel); }
Example 8
Source File: TrojanReceiver.java From Trojan with Apache License 2.0 | 5 votes |
private void showBatteryState(Intent intent) { int status = intent.getIntExtra("status", 0); int level = intent.getIntExtra("level", 0); String statusResult = "discharging"; switch (status) { case BatteryManager.BATTERY_STATUS_UNKNOWN: statusResult = "discharging"; break; case BatteryManager.BATTERY_STATUS_CHARGING: statusResult = "charging"; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: statusResult = "discharging"; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: statusResult = "discharging"; break; case BatteryManager.BATTERY_STATUS_FULL: statusResult = "charging"; break; } List<String> msgList = new LinkedList<>(); msgList.add(String.valueOf((level * 1.00 / 100))); msgList.add(statusResult); Trojan.log(LogConstants.BATTERY_TAG, msgList); }
Example 9
Source File: ViewUtils.java From native-navigation with MIT License | 5 votes |
/** * https://developer.android.com/training/monitoring-device-state/battery-monitoring.html */ public static int getBatteryState(Context context) { IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, ifilter); if (batteryStatus == null) { return BatteryManager.BATTERY_STATUS_UNKNOWN; } return batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); }
Example 10
Source File: DeviceState.java From 365browser with Apache License 2.0 | 5 votes |
/** @return integer representing the current status of the battery. */ @VisibleForTesting int getStickyBatteryStatus(Context context) { IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); // Call registerReceiver on context.getApplicationContext(), not on context itself, because // context could be a BroadcastReceiver context, which would throw an // android.content.ReceiverCallNotAllowedException. Intent batteryStatus = context.getApplicationContext().registerReceiver(null, iFilter); if (batteryStatus == null) { return BatteryManager.BATTERY_STATUS_UNKNOWN; } return batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN); }
Example 11
Source File: BatteryFragment.java From DeviceInfo with Apache License 2.0 | 4 votes |
private void getBatteryInfo() { if (fabBatteryCharging != null) { if (deviceStatus == BatteryManager.BATTERY_STATUS_CHARGING) { fabBatteryCharging.setVisibility(View.VISIBLE); fabBatteryCharging.setImageResource(R.drawable.ic_battery); } if (deviceStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) { fabBatteryCharging.setVisibility(View.GONE); } if (deviceStatus == BatteryManager.BATTERY_STATUS_FULL) { fabBatteryCharging.setVisibility(View.GONE); } if (deviceStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) { fabBatteryCharging.setVisibility(View.GONE); } if (deviceStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { fabBatteryCharging.setVisibility(View.GONE); } } final Handler handler = new Handler(); final Runnable runnable = new Runnable() { int counter = 0; @Override public void run() { try { if (counter <= level) { progressBar.setProgress(counter); progressBar.postDelayed(this, 10000); counter++; handler.postDelayed(this, 20); } else { handler.removeCallbacks(this); } } catch (NullPointerException e) { e.printStackTrace(); } } }; handler.postDelayed(runnable, 20); tvBatteryTemperature.setText("".concat(String.valueOf(temperature)).concat(mActivity.getResources().getString(R.string.c_symbol))); if (Validation.isRequiredField(technology)) { tvBatteryType.setText("".concat(technology)); } tvBatteryVoltage.setText("".concat(String.valueOf(voltage).concat("mV"))); tvBatteryScale.setText("".concat(String.valueOf(scale))); tvBatteryLevel.setText("".concat(String.valueOf(level)).concat("%")); if (health == 1) { tvBatteryHealth.setText(mResources.getString(R.string.unknown)); } else if (health == 2) { tvBatteryHealth.setText(mResources.getString(R.string.good)); } else if (health == 3) { tvBatteryHealth.setText(mResources.getString(R.string.over_heated)); } else if (health == 4) { tvBatteryHealth.setText(mResources.getString(R.string.dead)); } else if (health == 5) { tvBatteryHealth.setText(mResources.getString(R.string.over_voltage)); } else if (health == 6) { tvBatteryHealth.setText(mResources.getString(R.string.failed)); } else { tvBatteryHealth.setText(mResources.getString(R.string.cold)); } if (plugged == 1) { tvPowerSource.setText(mResources.getString(R.string.ac_power)); } else { tvPowerSource.setText(mResources.getString(R.string.battery)); } }
Example 12
Source File: BatteryInformation.java From Saiy-PS with GNU Affero General Public License v3.0 | 4 votes |
/** * Method to resolve the battery status */ private void getStatus() { if (DEBUG) { MyLog.i(CLS_NAME, "getStatus"); } if (batteryIntent != null) { final SaiyResources sr = new SaiyResources(mContext, sl); final int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN); switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_STATUS_UNKNOWN); switch (plugged) { case BatteryManager.BATTERY_PLUGGED_AC: setStatusResponse(sr.getString(ai.saiy.android.R.string.ac_charging)); break; case BatteryManager.BATTERY_PLUGGED_USB: setStatusResponse(sr.getString(ai.saiy.android.R.string.usb_charging)); break; default: setStatusResponse(sr.getString(ai.saiy.android.R.string.charging)); break; } break; case BatteryManager.BATTERY_STATUS_DISCHARGING: setStatusResponse(sr.getString(ai.saiy.android.R.string.discharging)); break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: setStatusResponse(sr.getString(ai.saiy.android.R.string.discharging)); break; case BatteryManager.BATTERY_STATUS_FULL: setStatusResponse(sr.getString(ai.saiy.android.R.string.fully_charged)); break; case BatteryManager.BATTERY_STATUS_UNKNOWN: setStatusResponse(sr.getString(ai.saiy.android.R.string.currently_indeterminable)); break; default: setStatusResponse(sr.getString(ai.saiy.android.R.string.currently_indeterminable)); break; } sr.reset(); } else { if (DEBUG) { MyLog.w(CLS_NAME, "batteryIntent: null"); } setAccessFailure(); } }
Example 13
Source File: Inspector.java From batteryhub with Apache License 2.0 | 4 votes |
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; }
Example 14
Source File: HostBatteryManager.java From DeviceConnect-Android with MIT License | 4 votes |
/** * バッテリーのIntentから情報を取得. */ public void getBatteryInfo() { IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus; int i = 0; do { batteryStatus = getContext().registerReceiver(null, filter); } while (i++ < 3 && batteryStatus == null); if (batteryStatus == null) { mStatusBattery = HostBatteryManager.BATTERY_STATUS_UNKNOWN; mValueLevel = 0; mValueScale = 0; return; } // バッテリーの変化を取得 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); switch (status) { case BatteryManager.BATTERY_STATUS_UNKNOWN: mStatusBattery = HostBatteryManager.BATTERY_STATUS_UNKNOWN; break; case BatteryManager.BATTERY_STATUS_CHARGING: mStatusBattery = HostBatteryManager.BATTERY_STATUS_CHARGING; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: mStatusBattery = HostBatteryManager.BATTERY_STATUS_DISCHARGING; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: mStatusBattery = HostBatteryManager.BATTERY_STATUS_NOT_CHARGING; break; case BatteryManager.BATTERY_STATUS_FULL: mStatusBattery = HostBatteryManager.BATTERY_STATUS_FULL; break; default: mStatusBattery = HostBatteryManager.BATTERY_STATUS_UNKNOWN; break; } // プラグの状態を取得 int plugged = batteryStatus.getIntExtra("plugged", 0); switch (plugged) { case BatteryManager.BATTERY_PLUGGED_AC: mStatusPlugged = BATTERY_PLUGGED_AC; break; case BatteryManager.BATTERY_PLUGGED_USB: mStatusPlugged = BATTERY_PLUGGED_USB; break; default: break; } // チャージングフラグ mChargingFlag = (plugged != 0); // バッテリー残量 mValueLevel = batteryStatus.getIntExtra("level", 0); mValueScale = batteryStatus.getIntExtra("scale", 0); }
Example 15
Source File: HostBatteryManager.java From DeviceConnect-Android with MIT License | 4 votes |
/** * バッテリーのIntentを設定. * * @param intent Batteryの変化で取得できたIntent */ private void setBatteryRequest(final Intent intent) { String mAction = intent.getAction(); if (Intent.ACTION_BATTERY_CHANGED.equals(mAction) || Intent.ACTION_BATTERY_LOW.equals(mAction) || Intent.ACTION_BATTERY_OKAY.equals(mAction)) { // バッテリーの変化を取得 int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); switch (status) { case BatteryManager.BATTERY_STATUS_UNKNOWN: mStatusBattery = HostBatteryManager.BATTERY_STATUS_UNKNOWN; break; case BatteryManager.BATTERY_STATUS_CHARGING: mStatusBattery = HostBatteryManager.BATTERY_STATUS_CHARGING; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: mStatusBattery = HostBatteryManager.BATTERY_STATUS_DISCHARGING; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: mStatusBattery = HostBatteryManager.BATTERY_STATUS_NOT_CHARGING; break; case BatteryManager.BATTERY_STATUS_FULL: mStatusBattery = HostBatteryManager.BATTERY_STATUS_FULL; break; default: mStatusBattery = HostBatteryManager.BATTERY_STATUS_UNKNOWN; break; } mValueLevel = intent.getIntExtra("level", 0); mValueScale = intent.getIntExtra("scale", 0); } else if (Intent.ACTION_POWER_CONNECTED.equals(mAction) || Intent.ACTION_POWER_DISCONNECTED.equals(mAction)) { mChargingFlag = Intent.ACTION_POWER_CONNECTED.equals(mAction); // プラグの状態を取得 int plugged = intent.getIntExtra("plugged", 0); switch (plugged) { case BatteryManager.BATTERY_PLUGGED_AC: mStatusPlugged = BATTERY_PLUGGED_AC; break; case BatteryManager.BATTERY_PLUGGED_USB: mStatusPlugged = BATTERY_PLUGGED_USB; break; default: break; } } }
Example 16
Source File: BatteryReceiver.java From MainScreenShow with GNU General Public License v2.0 | 4 votes |
@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(); }