恢复2a74fd2c304b571ab5ae349ffc3b7f06c5b4daf7提交点,旧版电量计算方法。

This commit is contained in:
2025-12-19 20:11:10 +08:00
parent bc6a82af41
commit e584e824c0

View File

@@ -1,80 +1,75 @@
package cc.winboll.studio.powerbell.utils;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.R;
/**
* 电池工具类:获取电池状态、发送提醒通知(复用逻辑,避免代码冗余)
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2024/07/18 04:32:46
* @Describe 电池状态工具类
* 功能解析电池广播Intent获取充电状态、当前电量
* 适配Java7 | API30 | 小米手机
*/
public class BatteryUtils {
private static final String TAG = "BatteryUtils";
private static final int REMIND_NOTIFICATION_ID = 10087; // 提醒通知ID
// ================================== 静态常量区(置顶归类,消除魔法值)=================================
public static final String TAG = "BatteryUtils";
/**
* 获取当前电池电量0-100
*/
public static int getCurrentBattery(Context context) {
Intent batteryIntent = context.getApplicationContext().registerReceiver(
null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)
);
if (batteryIntent == null) return 0;
// 提取电量(当前电量/总电量 * 100
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
return (level * 100) / scale;
}
// 电池电量计算常量
private static final int BATTERY_SCALE_DEFAULT = 100;
private static final int BATTERY_LEVEL_MIN = 0;
private static final int BATTERY_LEVEL_MAX = 100;
// ================================== 工具方法(静态方法,无状态设计)=================================
/**
* 判断是否正在充电(有线/无线均可)
* 判断当前是否处于充电状态
* @param intent 电池状态广播Intent非空
* @return true=充电中/已充满false=未充电
*/
public static boolean isCharging(Context context) {
Intent batteryIntent = context.getApplicationContext().registerReceiver(
null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)
);
if (batteryIntent == null) return false;
int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
return status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL;
}
/**
* 显示充电提醒通知
*/
public static void showChargeReminderNotification(Context context) {
Notification notification = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("充电提醒")
.setContentText("电池电量已达标,建议及时断电保护电池~")
.setAutoCancel(true) // 点击后取消通知
.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
manager.notify(REMIND_NOTIFICATION_ID, notification);
public static boolean isCharging(Intent intent) {
LogUtils.d(TAG, "isCharging: 调用 | intent=" + intent);
// 入参非空校验
if (intent == null) {
LogUtils.e(TAG, "isCharging: intent为空返回false");
return false;
}
LogUtils.d(TAG, "showChargeReminderNotification: notification show");
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
LogUtils.d(TAG, "isCharging: 解析完成 | status=" + status + " | result=" + isCharging);
return isCharging;
}
/**
* 显示耗电提醒通知
* 获取当前电池电量百分比0-100
* @param intent 电池状态广播Intent非空
* @return 电量百分比异常返回0
*/
public static void showUsageReminderNotification(Context context) {
Notification notification = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("耗电提醒")
.setContentText("电池电量偏低,建议及时充电~")
.setAutoCancel(true)
.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
manager.notify(REMIND_NOTIFICATION_ID, notification);
public static int getCurrentBatteryLevel(Intent intent) {
LogUtils.d(TAG, "getCurrentBatteryLevel: 调用 | intent=" + intent);
// 入参非空校验
if (intent == null) {
LogUtils.e(TAG, "getCurrentBatteryLevel: intent为空返回0");
return BATTERY_LEVEL_MIN;
}
LogUtils.d(TAG, "showUsageReminderNotification: notification show");
// 解析电量原始值与刻度值
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, BATTERY_LEVEL_MIN);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE_DEFAULT);
LogUtils.d(TAG, "getCurrentBatteryLevel: 原始值 | level=" + level + " | scale=" + scale);
// 计算并校验电量百分比避免除以0或数值越界
int batteryLevel;
if (scale <= 0) {
batteryLevel = level;
LogUtils.w(TAG, "getCurrentBatteryLevel: scale无效直接使用level值");
} else {
batteryLevel = level * BATTERY_SCALE_DEFAULT / scale;
}
// 确保电量值在0-100范围内
batteryLevel = Math.max(BATTERY_LEVEL_MIN, Math.min(batteryLevel, BATTERY_LEVEL_MAX));
LogUtils.d(TAG, "getCurrentBatteryLevel: 计算完成 | batteryLevel=" + batteryLevel + "%");
return batteryLevel;
}
}