ENG*_*618 2 android broadcastreceiver batterylevel
我正在创建一个电池监视器应用程序,并且尝试显示当前的电池百分比。我有一个广播接收器,正在监听...
<action android:name="android.intent.action.BATTERY_CHANGED" />
Run Code Online (Sandbox Code Playgroud)
在接收器中我有以下代码。
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();
Run Code Online (Sandbox Code Playgroud)
由于某种原因,结果总是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>
Run Code Online (Sandbox Code Playgroud)
我的接收器是...
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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我认为已经很晚了,但解决方案很简单。根据此链接,如果您在 xml 中注册了广播接收器,您将不会收到 BATTERY_CHANGED 的更新,因为它是粘性广播。
您需要在 java 程序中注册广播接收器,如下所示。
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;
Run Code Online (Sandbox Code Playgroud)
注意:即使您将“null”传递为接收器,您仍然可以接收所有所需的电池数据。您还可以传递 Receiver 类而不是“null”来进行连续监控,但请注意,这会耗尽您的设备电池,因为这些广播经常抛出。
| 归档时间: |
|
| 查看次数: |
10105 次 |
| 最近记录: |