由于内部更改,CurrentWidget在牛轧糖及更高版本上不起作用

时间:2018-09-28 06:42:08

标签: android android-permissions battery

我需要一些帮助来获取手机中的电池电流。

我尝试通过以下信息来获取电池电流(答案):

Getting the battery current values for the Android Phone

当前Widget在Android 7.0以下的android版本上运行,但由于Android内部变化,因此在Android N或更高版本上无法使用。可以检查此链接。

https://play.google.com/store/apps/details?id=com.manor.currentwidget&hl=en_US

如果尝试在android 7.0设备上使用当前小部件,将无法访问sys文件。 这是获取电池电流的替代方法吗?

ps: 方法IntentFilter(Intent.ACTION_BATTERY_CHANGED)和bundle.getInt(“ current_avg”)不返回任何值

1 个答案:

答案 0 :(得分:0)

  

使用此代码查看电池电量百分比。

public static int getBatteryPercentage(Context context) {

       IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
       Intent batteryStatus = context.registerReceiver(null, iFilter);

       int level = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1;
       int scale = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;

       float batteryPct = level / (float) scale;

       return (int) (batteryPct * 100);
   }