我怎样才能电池电量和状态(插入,放电,充电等)? 我研究了开发文档,我发现一个BatteryManager类。 但它不包含任何方法,只要常量。 如何我甚至用它?
Answer 1:
这里是解释如何获得电池信息的代码示例。
概括起来讲,对于广播接收机ACTION_BATTERY_CHANGED
意图建立动态的,因为它无法通过清单中声明组件接收,仅通过用显式登记为它Context.registerReceiver()
public class Main extends Activity {
private TextView batteryTxt;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context ctxt, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
batteryTxt.setText(String.valueOf(level) + "%");
}
};
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main);
batteryTxt = (TextView) this.findViewById(R.id.batteryTxt);
this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
}
Answer 2:
由于API 21日已经可以使用以下方法来获得当前电池电量为百分比:
BatteryManager bm = (BatteryManager)getSystemService(BATTERY_SERVICE);
int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
Answer 3:
基于官方Android文档 ,您可以使用此方法在助手或的Util类来获得当前电池百分比:
public static int getBatteryPercentage(Context context) {
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, iFilter);
int level = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1;
int scale = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;
float batteryPct = level / (float) scale;
return (int) (batteryPct * 100);
}
Answer 4:
您没有使用粘性意向登记的实际广播接收器作为Android的BatteryManager:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
return (int)(batteryPct*100);
这是从官方文档上的https://developer.android.com/training/monitoring-device-state/battery-monitoring.html 。
Answer 5:
你可以用它来获得剩余百分比收取费用。
private void batteryLevel() {
BroadcastReceiver 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;
}
batterLevel.setText("Battery Level Remaining: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
Answer 6:
试试这个功能不需要在未经许可或任何reciver
void getBattery_percentage()
{
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = getApplicationContext().registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
float p = batteryPct * 100;
Log.d("Battery percentage",String.valueOf(Math.round(p)));
}
Answer 7:
一些经过研究,我发现BatteryManager.EXTRA_LEVEL。
Answer 8:
private void batteryLevel() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra("level", -1);
int scale = intent.getIntExtra("scale", -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
mBtn.setText("Battery Level Remaining: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
Answer 9:
要检查我们使用BatteryManager电量百分比,下面的方法将返回电量百分比。
源链接
public static float getBatteryLevel(Context context, Intent intent) {
Intent batteryStatus = context.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;
}
Answer 10:
在官方的文档一个错误,则必须使用双,而不是浮动。 因为0.53F * 100F = 52F
int level = batteryStatus != null ?
batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1; int scale =
batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;
double batteryPct = (double) level / (double) scale; int percent = (int) (batteryPct * 100D);
文章来源: Get battery level and state in Android