-1

I am trying to get the battery information mainly power left, voltage, current for an android N(7.1.1) device. I am trying to run the following code in onCreate method.

First approach;

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        batteryStatus = this.registerReceiver(null, ifilter);
        double currentNow = BatteryManager.BATTERY_PROPERTY_CURRENT_NOW;
        double voltage = BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE;
        double level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        double scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, 

Second approach:

 BatteryManager mBatteryManager =
                (BatteryManager)Context.getSystemService(Context.BATTERY_SERVICE);
        Long energy =
                mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);

First approach is giving me the default values and second approach is giving me error: non-static method getsystemservice(string) cannot be referenced from a static context

Digvijaysinh Gohil
  • 1,367
  • 2
  • 15
  • 30

1 Answers1

1

Have you tried someting like this:

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get the battery scale
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);

        // get the battery level
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);

    }
};

And in your on create method:

IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);    
mContext.registerReceiver(mBroadcastReceiver,iFilter);
Ram Koti
  • 2,203
  • 7
  • 26
  • 36
Tarek
  • 708
  • 5
  • 14
  • What is the variable mContext? – user2782345 Feb 28 '18 at 17:47
  • @user2782345 It likely stores the context of wherever you're registering the broadcast receiver. So for an activity, you could replace `mContext` with `this` (or just call `registerReceiver` without anything preceding it). – pushasha Feb 28 '18 at 17:55
  • Is there a way to get the power left in the phone in nano-watt hours i found this on the https://developer.android.com/reference/android/os/BatteryManager.html#BATTERY_PROPERTY_ENERGY_COUNTER – user2782345 Feb 28 '18 at 18:30
  • @user2782345 You would probably do it exactly like you do in your question (with BatteryManager), but don't call it from a static context. – pushasha Feb 28 '18 at 19:25
  • @pushasha I dont know how to not call it from a static context. What does it mean? Am I calling the method from a non static method and it has to be a static. – user2782345 Feb 28 '18 at 20:47
  • @user2782345 You're using the static `Context` to call `getSystemService()`. Try using a non-static `Context` reference (such as `this`, in an activity or view). See the answers to [this question](https://stackoverflow.com/questions/17992637/cannot-make-a-static-reference-to-the-non-static-method-getsystemservicestring). – pushasha Feb 28 '18 at 21:31