BATTERY_PROPERTY_CURRENT_NOW does not work on every device. What are the alternatives?

My application uses BatteryManager.BATTERY_PROPERTY_CURRENT_NOW to get the device’s battery current:

BatteryManager batteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
int current = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW), context);

However, it does not work, for example, on the Samsung Galaxy Tab. There it returns only 0.

Other applications show current even on this device. How do they do it? What are the alternatives to my method?

+9
source share
5 answers

From your question ("returns only 0"), please see the documentation for getIntProperty: https://developer.android.com/reference/android/os/BatteryManager#getIntProperty(int)

- ,

(a) 0, targetSdkVersion <VERSION_CODES.P

(b) Integer.MIN_VALUE, targetSdkVersion> = VERSION_CODES.P.

0

:

int getBatteryPercent(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (intent != null) {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        return level * 100 / scale;
    }
    return 0;
}
0

I am using Samsung Galaxy Tab A (SM-T580). Please use the method below.

 int  batteryPercentage = getBatteryPercentage(MainActivity.this);


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

            int bb = (int) (batteryPct * 100);
            return bb ;
        }

Output:

enter image description here

enter image description here

0
source

You can get the current battery level using the broadcast receiver, for the ACTION_BATTERY_CHANGEDintention is ACTION_BATTERY_CHANGEDset dynamically withContext.registerReceiver().

public class Main extends Activity {
  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context ctxt, Intent intent) {
      int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    }
  };

  @Override
  public void onCreate(Bundle SavedInstanceStat) {
    super.onCreate(SavedInstanceStat);
    setContentView(R.layout.main);
    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  }
}
0
source

Try it:

BatteryManager batteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);

    int current = batteryManager.getIntProperty(BatteryManager.EXTRA_LEVEL), context)
-2
source

Source: https://habr.com/ru/post/1680109/


All Articles