本文整理匯總了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;
}