Java Code Examples for android.os.BatteryManager#BATTERY_STATUS_NOT_CHARGING

The following examples show how to use android.os.BatteryManager#BATTERY_STATUS_NOT_CHARGING . 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 vote down vote up
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: BatteryInfo.java    From MobileInfo with Apache License 2.0 6 votes vote down vote up
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 3
Source File: SKBatteryData.java    From SensingKit-Android with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 4
Source File: UENavigationFragmentActivity.java    From Auie with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	int level = intent.getIntExtra("level", -1);
          int status = intent.getIntExtra("status", -1);
          switch (status) {
	case BatteryManager.BATTERY_STATUS_FULL:
		mNavigationView.setStatus(UIBatteryView.STATUS_COMLETED);
		mNavigationView.setBatteryText("已充满");
		break;
	case BatteryManager.BATTERY_STATUS_CHARGING:
		mNavigationView.setStatus(UIBatteryView.STATUS_CHARGED);
		mNavigationView.setBatteryText("充电中");
		break;
	case BatteryManager.BATTERY_STATUS_DISCHARGING:
	case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
		mNavigationView.setLevel(level * 0.01f);
		mNavigationView.setBatteryText(level + "%");
		break;
	default:
		mNavigationView.setLevel(0);
		mNavigationView.setBatteryText("无电池");
		break;
	}
}
 
Example 5
Source File: UENavigationActivity.java    From Auie with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	int level = intent.getIntExtra("level", -1);
          int status = intent.getIntExtra("status", -1);
          switch (status) {
	case BatteryManager.BATTERY_STATUS_FULL:
		mNavigationView.setStatus(UIBatteryView.STATUS_COMLETED);
		mNavigationView.setBatteryText("已充满");
		break;
	case BatteryManager.BATTERY_STATUS_CHARGING:
		mNavigationView.setStatus(UIBatteryView.STATUS_CHARGED);
		mNavigationView.setBatteryText("充电中");
		break;
	case BatteryManager.BATTERY_STATUS_DISCHARGING:
	case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
		mNavigationView.setLevel(level * 0.01f);
		mNavigationView.setBatteryText(level + "%");
		break;
	default:
		mNavigationView.setLevel(0);
		mNavigationView.setBatteryText("无电池");
		break;
	}
}
 
Example 6
Source File: DeviceState.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * Conversion from charging status int to String can be done through this method.
 *
 * @param status integer representing the charging status.
 * @return String representing the charging status.
 */
private String getStatus(int status) {
    String statusString = UNKNOWN;
    switch (status) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
            statusString = CHARGING;
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            statusString = DISCHARGING;
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            statusString = FULL;
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            statusString = NOT_CHARGING;
            break;
    }
    return statusString;
}
 
Example 7
Source File: TypeTranslators.java    From under-the-hood with Apache License 2.0 6 votes vote down vote up
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 8
Source File: BatteryChangeReceiver.java    From CacheEmulatorChecker with Apache License 2.0 6 votes vote down vote up
@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 9
Source File: BatteryService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private int getIconLocked(int level) {
    if (mHealthInfo.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
        return com.android.internal.R.drawable.stat_sys_battery_charge;
    } else if (mHealthInfo.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
        return com.android.internal.R.drawable.stat_sys_battery;
    } else if (mHealthInfo.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
            || mHealthInfo.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
        if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
                && mHealthInfo.batteryLevel >= 100) {
            return com.android.internal.R.drawable.stat_sys_battery_charge;
        } else {
            return com.android.internal.R.drawable.stat_sys_battery;
        }
    } else {
        return com.android.internal.R.drawable.stat_sys_battery_unknown;
    }
}
 
Example 10
Source File: RetroWatchService.java    From retrowatch with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	String action = intent.getAction();
	if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
		int plugType = intent.getIntExtra("plugged", 0);
		int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
		
		int chargingStatus = EmergencyObject.BATT_STATE_UNKNOWN;
		if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
			if (plugType > 0) {
				chargingStatus = ((plugType == BatteryManager.BATTERY_PLUGGED_AC) 
						? EmergencyObject.BATT_STATE_AC_CHARGING : EmergencyObject.BATT_STATE_USB_CHARGING);
			}
		} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
			chargingStatus = EmergencyObject.BATT_STATE_DISCHARGING;
		} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
			chargingStatus = EmergencyObject.BATT_STATE_NOT_CHARGING;
		} else if (status == BatteryManager.BATTERY_STATUS_FULL) {
			chargingStatus = EmergencyObject.BATT_STATE_FULL;
		} else {
			chargingStatus = EmergencyObject.BATT_STATE_UNKNOWN;
		}
		
		int level = intent.getIntExtra("level", 0);
		int scale = intent.getIntExtra("scale", 100);
		
		// WARNING: Battery service makes too many broadcast.
		// Process data only when there's change in battery level or status.
		if(mContentManager.getBatteryLevel() == level
				&& mContentManager.getBatteryChargingState() == status)
			return;
		
		ContentObject co = mContentManager.setBatteryInfo(level * 100 / scale, chargingStatus);
		if(co != null)
			sendContentsToDevice(co);
	}
}
 
Example 11
Source File: TrojanReceiver.java    From Trojan with Apache License 2.0 5 votes vote down vote up
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 12
Source File: BatteryInformation.java    From Saiy-PS with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * 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: GalleryActivity.java    From HHComicViewer with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)
            && mIv_battery != null) {
        //得到系统当前电量
        int level = intent.getIntExtra("level", 0);
        int status = intent.getIntExtra("status", 0);
        boolean chargingFlag = false;
        switch (status) {
            case BatteryManager.BATTERY_STATUS_CHARGING:
                //充电中
                chargingFlag = true;
                break;
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                //放电中
                chargingFlag = false;
                break;
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                //未充电
                chargingFlag = false;
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                //电量满
                chargingFlag = false;
                mIv_battery.setImageResource(R.mipmap.battery_full);
                break;
        }
        if (level == 100) {
            if (chargingFlag) {
                mIv_battery.setImageResource(R.mipmap.battery_charging_full);
            } else {
                mIv_battery.setImageResource(R.mipmap.battery_full);
            }
        } else if (level >= 90 && level < 100) {
            if (chargingFlag) {
                mIv_battery.setImageResource(R.mipmap.battery_charging_90);
            } else {
                mIv_battery.setImageResource(R.mipmap.battery_90);
            }
        } else if (level >= 80 && level < 90) {
            if (chargingFlag) {
                mIv_battery.setImageResource(R.mipmap.battery_charging_80);
            } else {
                mIv_battery.setImageResource(R.mipmap.battery_80);
            }
        } else if (level >= 60 && level < 80) {
            if (chargingFlag) {
                mIv_battery.setImageResource(R.mipmap.battery_charging_60);
            } else {
                mIv_battery.setImageResource(R.mipmap.battery_60);
            }
        } else if (level >= 50 && level < 60) {
            if (chargingFlag) {
                mIv_battery.setImageResource(R.mipmap.battery_charging_50);
            } else {
                mIv_battery.setImageResource(R.mipmap.battery_50);
            }
        } else if (level >= 30 && level < 50) {
            if (chargingFlag) {
                mIv_battery.setImageResource(R.mipmap.battery_charging_30);
            } else {
                mIv_battery.setImageResource(R.mipmap.battery_30);
            }
        } else if (level >= 20 && level < 30) {
            if (chargingFlag) {
                mIv_battery.setImageResource(R.mipmap.battery_charging_20);
            } else {
                mIv_battery.setImageResource(R.mipmap.battery_20);
            }
        } else if (level < 20) {
            if (chargingFlag) {
                mIv_battery.setImageResource(R.mipmap.battery_charging_20);
            } else {
                mIv_battery.setImageResource(R.mipmap.battery_alert);
            }
        }
    }
}
 
Example 14
Source File: Inspector.java    From batteryhub with Apache License 2.0 4 votes vote down vote up
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 15
Source File: HostBatteryManager.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * バッテリーの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 16
Source File: HostBatteryManager.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * バッテリーの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 17
Source File: BatteryReceiver.java    From MainScreenShow with GNU General Public License v2.0 4 votes vote down vote up
@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();
 }
 
Example 18
Source File: BatteryFragment.java    From DeviceInfo with Apache License 2.0 4 votes vote down vote up
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 19
Source File: RetroWatchService.java    From retrowatch with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	String action = intent.getAction();
	if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
		int plugType = intent.getIntExtra("plugged", 0);
		int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
		
		int chargingStatus = EmergencyObject.BATT_STATE_UNKNOWN;
		if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
			if (plugType > 0) {
				chargingStatus = ((plugType == BatteryManager.BATTERY_PLUGGED_AC) 
						? EmergencyObject.BATT_STATE_AC_CHARGING : EmergencyObject.BATT_STATE_USB_CHARGING);
			}
		} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
			chargingStatus = EmergencyObject.BATT_STATE_DISCHARGING;
		} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
			chargingStatus = EmergencyObject.BATT_STATE_NOT_CHARGING;
		} else if (status == BatteryManager.BATTERY_STATUS_FULL) {
			chargingStatus = EmergencyObject.BATT_STATE_FULL;
		} else {
			chargingStatus = EmergencyObject.BATT_STATE_UNKNOWN;
		}
		
		int level = intent.getIntExtra("level", 0);
		int scale = intent.getIntExtra("scale", 100);
		
		Logs.d("# mBatteryInfoReceiver : level = " + level);
		
		// WARNING: Battery service makes too many broadcast.
		// Process data only when there's change in battery level or status.
		if(mContentManager.getBatteryLevel() == level
				&& mContentManager.getBatteryChargingState() == status)
			return;
		
		ContentObject co = mContentManager.setBatteryInfo(level, chargingStatus);
		if(co != null && level < 10) {
			Logs.d("# mBatteryInfoReceiver - reserve update");
			reserveRemoteUpdate(DEFAULT_UPDATE_DELAY);
		}
	}
}