gpt4 book ai didi

android - 如何获取当前电池百分比?

转载 作者:行者123 更新时间:2023-11-29 00:13:52 29 4
gpt4 key购买 nike

我正在创建一个电池监视器应用程序,我正在尝试显示当前的电池电量百分比。我有一个广播接收器,正在收听...

<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%...我哪里出错了?

编辑:

接收者在 list 中注册为...

<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>

我的接收器是...

包 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;
}
}
}
}

最佳答案

我认为已经很晚了,但解决方案非常简单。根据this link ,如果您在 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;

注意:即使您将“null”作为接收器传递,您仍然可以接收所有必需的电池数据。您还可以传递一个 Receiver 类而不是“null”来进行持续监控,但要注意这会耗尽您的设备电池,因为这些广播经常被抛出。

关于android - 如何获取当前电池百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28006731/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com