0

i need some help to get battery current in phone.

I try to retrieve battery current by following this post (Answer):

Getting the battery current values for the Android Phone

Current Widget is working on android version below Android 7.0,but it doesn't work on Android N and above due to Android internal changes. Can check this link.

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

If try current widget on android 7.0 device will get access denied for sys file. Is that any alternative way to get battery current?

ps: method IntentFilter(Intent.ACTION_BATTERY_CHANGED) and bundle.getInt("current_avg") return no value

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Minipika
  • 31
  • 1
  • 2

1 Answers1

0

Use this code to see battery percentage.

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