我正在创建一个电池监视器应用程序,我正在尝试显示当前的电池百分比。我有一个广播接收器,正在听...
<action android:name="android.intent.action.BATTERY_CHANGED" />
在接收器中我有以下代码。
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
currentBatteryLevel = level / (float) scale;
Toast.makeText(context, "Current battery level: " + Float.toString(currentBatteryLevel) + "%", Toast.LENGTH_SHORT).show();
出于某种原因,结果总是Current battery level: 1.0%
......我哪里出错?
编辑:
接收方在清单中注册为......
<receiver android:name=".PowerMonitorReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.intent.action.BATTERY_LOW" />
<action android:name="android.intent.action.BATTERY_OKAY" />
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
我的接收器是......
package com.garciaericn.t2d;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
import android.widget.Toast;
public class PowerMonitorReceiver extends BroadcastReceiver {
private static final String TAG = "PowerMonitorReceiver.TAG";
private float currentBatteryLevel;
@Override
public void onReceive(Context context, Intent intent) {
// Obtain action from intent to check which broadcast is being received
String action = intent.getAction();
// Perform action according to type
switch (action) {
case (Intent.ACTION_BATTERY_CHANGED): {
Log.i(TAG, "Battery has changed");
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
currentBatteryLevel = (level * 100) / (float) scale;
Toast.makeText(context, "Current battery level: " + Float.toString(currentBatteryLevel) + "%" + " level: " + level + " scale: " + scale, Toast.LENGTH_LONG).show();
break;
}
case (Intent.ACTION_POWER_CONNECTED): {
Log.i(TAG, "Power connected");
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;
String chargingType = null;
if (usbCharge) {
chargingType = "USB";
} else if (acCharge) {
chargingType = "AC Power";
}
if (isCharging && chargingType != null) {
Toast.makeText(context, "Device is charging via: " + chargingType, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Device is charging.", Toast.LENGTH_SHORT).show();
}
break;
}
case (Intent.ACTION_POWER_DISCONNECTED): {
Log.i(TAG, "Power disconnected");
Toast.makeText(context, "Power Disconnected", Toast.LENGTH_SHORT).show();
break;
}
case (Intent.ACTION_BATTERY_LOW): {
Log.i(TAG, "Battery low");
Toast.makeText(context, "Battery low", Toast.LENGTH_SHORT).show();
break;
}
case (Intent.ACTION_BATTERY_OKAY): {
Log.i(TAG, "Battery Okay");
Toast.makeText(context, "Battery Okay", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
答案 0 :(得分:2)
我认为现在已经很晚了,但解决方案非常简单。根据{{3}},如果您已在xml中注册广播接收器,则不会收到BATTERY_CHANGED的更新,因为它是一个粘性广播。
您需要在java程序中注册Broadcastreceivers,如下所示。
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = (level * 100) / (float)scale;
注意:即使你通过&#34; null&#34;作为接收器,您仍然可以接收所有必需的电池数据。您也可以传递一个Receiver类而不是&#34; null&#34;对于持续监控但请注意,这会耗尽您的设备电池,因为这些广播经常被抛出。
答案 1 :(得分:0)
由于您正在收听多个广播,因此您需要确定它是哪种类型的广播。
String action = intent.getAction();
if(action.equals(Intent.ACTION_BATTERY_CHANGED)){
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
int percent = (level*100)/scale;
Toast.makeText(context, "Current battery level: " + percent + "%", Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
boolean acCharge = false;
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
if(isCharging){
chargePercentage =
batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
}
chargePercentage变量将为您提供当前的充电百分比。