本文整理汇总了Java中android.os.BatteryManager类的典型用法代码示例。如果您正苦于以下问题:Java BatteryManager类的具体用法?Java BatteryManager怎么用?Java BatteryManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BatteryManager类属于android.os包,在下文中一共展示了BatteryManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onReceive
import android.os.BatteryManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
// 接收电量变化信息
if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);
// 电量百分比
int curPower = level * 100 / scale;
int status = intent.getIntExtra("status", BatteryManager.BATTERY_HEALTH_UNKNOWN);
// SecondaryProgress 用来展示低电量,Progress 用来展示正常电量
if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
mPbBatteryLevel.setSecondaryProgress(0);
mPbBatteryLevel.setProgress(curPower);
mPbBatteryLevel.setBackgroundResource(R.mipmap.ic_battery_charging);
} else if (curPower < BATTERY_LOW_LEVEL) {
mPbBatteryLevel.setProgress(0);
mPbBatteryLevel.setSecondaryProgress(curPower);
mPbBatteryLevel.setBackgroundResource(R.mipmap.ic_battery_red);
} else {
mPbBatteryLevel.setSecondaryProgress(0);
mPbBatteryLevel.setProgress(curPower);
mPbBatteryLevel.setBackgroundResource(R.mipmap.ic_battery);
}
}
}
示例2: setCharging
import android.os.BatteryManager; //导入依赖的package包/类
@SuppressWarnings("deprecation")
public static void setCharging(Context context, boolean charging) {
Intent chargingIntent = new Intent(Intent.ACTION_BATTERY_CHANGED);
if (charging) {
chargingIntent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_AC);
}
context.sendStickyBroadcast(chargingIntent);
}
示例3: onReceive
import android.os.BatteryManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
// 0 indicates that we're on battery
boolean onBatteryNow = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) <= 0;
if (onBatteryNow) {
InactivityTimer.this.onActivity();
} else {
InactivityTimer.this.cancel();
}
}
}
示例4: onReceive
import android.os.BatteryManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
// 0 indicates that we're on battery
boolean onBatteryNow = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) <= 0;
if (onBatteryNow) {
InactivityTimer.this.onActivity();
} else {
InactivityTimer.this.cancel();
}
}
}
示例5: batteryLevelUpdater
import android.os.BatteryManager; //导入依赖的package包/类
private void batteryLevelUpdater()
{
batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
mBatteryLevelView.setLevel(level);
}
};
batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
示例6: getBatteryLevel
import android.os.BatteryManager; //导入依赖的package包/类
private float getBatteryLevel() {
Intent batteryStatus = registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int batteryLevel = -1;
int batteryScale = 1;
if (batteryStatus != null) {
batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, batteryLevel);
batteryScale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, batteryScale);
}
return batteryLevel / (float) batteryScale * 100;
}
示例7: onReceive
import android.os.BatteryManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == Intent.ACTION_BATTERY_CHANGED) {
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
float temperature = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)) / 10;
float chargeLevel = ((float) (level * 100)) / scale;
for (SensorInfo sensorInfo : sensorInfos) {
if (sensorInfo.getType() == SensorType.BATTERY) {
sensorInfo.setValues(new float[]{temperature, chargeLevel});
break;
}
}
}
}
示例8: handleBattery
import android.os.BatteryManager; //导入依赖的package包/类
private void handleBattery(Intent intent){
int level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
int scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
int levelPercent = (int)(((float)level / scale) * 100);
boolean charging = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN) == BatteryManager.BATTERY_STATUS_CHARGING;
if(levelPercent < BATTERY_LOW_LIMIT && !charging)
AppModel.sDevBatteryLow = true;
else
AppModel.sDevBatteryLow = false;
LogUtils.i("battery:" + level +"%, BatteryLow=" + AppModel.sDevBatteryLow);
}
示例9: getBatteryLevel
import android.os.BatteryManager; //导入依赖的package包/类
/**
* Returns the current device battery level.
*/
static String getBatteryLevel(Context context) {
try {
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
if(batteryIntent != null) {
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
// Error checking that probably isn't needed but I added just in case.
if (level > -1 && scale > 0) {
return Float.toString(((float) level / (float) scale) * 100.0f);
}
}
}
catch(Exception e){
if (Countly.sharedInstance().isLoggingEnabled()) {
Log.i(Countly.TAG, "Can't get batter level");
}
}
return null;
}
示例10: recordBatteryLevel
import android.os.BatteryManager; //导入依赖的package包/类
private void recordBatteryLevel() {
Log.i(TAG, "Record battery level");
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = mContext.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = (level / (float) scale) * 100;
mTelemetry.write(Telemetry.BATTERY_TAG, String.valueOf(batteryPct));
}
示例11: onReceive
import android.os.BatteryManager; //导入依赖的package包/类
@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;
}
}
}
示例12: registerBatInforReceiver
import android.os.BatteryManager; //导入依赖的package包/类
private void registerBatInforReceiver() {
mBatInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
if ((System.currentTimeMillis() - lastUpdateSensorTemperature) < Utils.SAMPLE_MILLIS) {
return;
}
int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10;
temperature_value = "" + temperature;
bt_temperature_sample.setEnabled(true);
bt_temperature_sample.setText(temperature_value + getResources().getString(R.string.battery_temp));
lastUpdateSensorTemperature = System.currentTimeMillis();
}
};
registerReceiver(mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
示例13: filterCommand
import android.os.BatteryManager; //导入依赖的package包/类
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean filterCommand(InterruptionCommand command) {
if (command.shouldNotVibrateOnCharger()) {
Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
boolean pluggedIn = batteryIntent != null && batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
if (pluggedIn) {
Timber.d("Filter - Charger!");
return false;
}
}
if (command.shouldNotVibrateInTheater() &&
WatchUtils.isWatchInTheaterMode(this)) {
Timber.d("Filter - Theater mode!");
return false;
}
return true;
}
示例14: onReceive
import android.os.BatteryManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
if (level != -1) {
_batteryTextView.setText(String.format(Locale.getDefault(), "%d %%", level));
_batteryAlarmView.setVisibility(View.VISIBLE);
if (level > BatterySocketController.UPPER_BATTERY_LIMIT) {
_batteryAlarmView.setBackgroundResource(R.drawable.circle_green);
} else if (level < BatterySocketController.LOWER_BATTERY_LIMIT) {
_batteryAlarmView.setBackgroundResource(R.drawable.circle_red);
} else {
_batteryAlarmView.setBackgroundResource(R.drawable.circle_green);
}
} else {
_batteryTextView.setText("Error!");
_batteryAlarmView.setVisibility(View.GONE);
}
}
示例15: getBatteryPercent
import android.os.BatteryManager; //导入依赖的package包/类
private float getBatteryPercent() {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = RYLA.getInstance().getContext().registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
return level / (float)scale * 100;
}