Android N(7.1.1)编程获取电池信息遇问题求助
Hey there! Let's work through your battery info issues step by step— I’ve run into these exact snags before, so here’s what you need to fix things:
首先解决「non-static method getsystemservice(string) cannot be referenced from a static context」报错
This error pops up because getSystemService() is a non-static method belonging to the Context class. You can’t call it directly from a static method, or using your activity class name (like YourActivity.getSystemService()).
Fix: Use your Activity's context (since Activity extends Context)
In your onCreate() method (which runs in a non-static context), just call it directly with your activity instance:
// 正确的调用方式 BatteryManager batteryManager = (BatteryManager) getSystemService(Context.BATTERY_SERVICE);
No static tricks needed—your activity already has access to the context it needs.
然后解决「第一种方案仅返回默认值」的问题
Most of the time, default values mean you’re using an outdated or incorrect method to fetch battery data. For Android 7.1.1, there are two reliable ways to get battery info: using the BatteryManager API, or grabbing the sticky ACTION_BATTERY_CHANGED intent.
Option 1: Use BatteryManager for real-time metrics
This is the cleanest approach for basic info like level, voltage, and current:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取BatteryManager实例(用当前Activity的上下文) BatteryManager batteryManager = (BatteryManager) getSystemService(Context.BATTERY_SERVICE); // 剩余电量(范围0-100,直接返回百分比) int batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); Log.d("BatteryStats", "剩余电量: " + batteryLevel + "%"); // 电池电压(单位微伏,转换为毫伏除以1000) int voltage = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_VOLTAGE); Log.d("BatteryStats", "电池电压: " + voltage / 1000 + "mV"); // 当前电流(单位微安,部分设备不支持,返回-1表示无法获取) int current = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW); if (current != -1) { Log.d("BatteryStats", "实时电流: " + current / 1000 + "mA"); } else { Log.d("BatteryStats", "该设备不支持获取实时电流"); } }
Option 2: Use the ACTION_BATTERY_CHANGED sticky intent
This gives you more detailed info (like charging status, health) and works reliably across most devices:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 注册临时接收器获取当前电池状态(不需要持久注册,因为这是粘性广播) IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryIntent = registerReceiver(null, batteryFilter); if (batteryIntent != null) { // 计算剩余电量百分比 int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPercent = (level / (float) scale) * 100; Log.d("BatteryStats", "剩余电量: " + String.format("%.1f", batteryPercent) + "%"); // 电池电压(直接返回毫伏) int voltage = batteryIntent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); Log.d("BatteryStats", "电池电压: " + voltage + "mV"); // 实时电流(单位微安,部分设备支持) int current = batteryIntent.getIntExtra(BatteryManager.EXTRA_CURRENT_NOW, -1); if (current != -1) { Log.d("BatteryStats", "实时电流: " + current / 1000 + "mA"); } // 检查充电状态 int chargeStatus = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = chargeStatus == BatteryManager.BATTERY_STATUS_CHARGING || chargeStatus == BatteryManager.BATTERY_STATUS_FULL; Log.d("BatteryStats", "是否充电中: " + isCharging); } }
Why your first method returned default values?
Chances are:
- You tried to get the
BatteryManagerfrom a static context (leading to an invalid instance) - You used outdated constants or methods that no longer work on Android N
- For current metrics, your device might not support the
BATTERY_PROPERTY_CURRENT_NOWproperty (which is common on older devices)
Quick Notes:
- You don’t need any special permissions to access this battery info on Android 7.1.1
- The sticky broadcast approach doesn’t require unregistering the receiver since we’re passing
nullas the receiver instance
内容的提问来源于stack exchange,提问作者user2782345

