如何在android中将手机的batterylevel显示为textview?
答案 0 :(得分:1)
在提出任何问题之前,请先使用Google。通过方式点击Here 我希望它可以帮助你。
答案 1 :(得分:0)
您可以获得教程检查here
答案 2 :(得分:0)
教程为Here。特别是:
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
答案 3 :(得分:0)
最好的方法是使用广播接收器,因为每次电池状态发生变化时都会触发onRecieve,因此当电量下降时,您的值将始终准确。
在您的活动的onCreate中,您需要为电池状态注册广播接收器:
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
batteryMonitor = new batteryChargeMonitor();
getActivity().registerReceiver(batteryMonitor, filter);
batteryMonitor类看起来像这样:
private class batteryChargeMonitor extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
if(status > -1 && scale > -1 && level > -1)
{
float charge = (level/(float)scale) * 1oo;
Toast.makeText(context, "Battery Charge: " + charge + "%" Toast.LENGTH_LONG).show();
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL;
}
}
}
答案 4 :(得分:0)
如果您只想以百分比显示当前电池电量,则无需注册接收器等。这样就可以了:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = getActivity().registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
我的设备的电池电量目前为94% - batteryPct的值为0.94。
然后可以在TextView中显示:
textView.setText(Float.toString(batteryPct * 100) + "%"); // Displays '94.0%' using my example value
请参阅:http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
答案 5 :(得分:0)
来自此处的链接link
接收器
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
}
}
AndroidManifest.xml添加如下
代码更改
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
//我们如何收费?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;