react-native-device-battery icon indicating copy to clipboard operation
react-native-device-battery copied to clipboard

Battery doest not work when app in background

Open Shelestenko opened this issue 6 years ago • 8 comments

Battery doest not work in android when app in background (minimized). I have error:

error Error: Battery manager is not active
    at createErrorFromErrorData (E:\projects\project_name\node_modules\react-native\Libraries\BatchedBridge\NativeModules.js:123)
    at E:\projects\project_name\node_modules\react-native\Libraries\BatchedBridge\NativeModules.js:80
    at MessageQueue.__invokeCallback (E:\projects\project_name\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:400)
    at E:\projects\project_name\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:139
    at MessageQueue.__guardSafe (E:\projects\project_name\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:316)
    at MessageQueue.invokeCallbackAndReturnFlushedQueue (E:\projects\project_name\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:138)
    at debuggerWorker.js:72

Please fix it. I use "react-native": "0.54.4", "react-native-background-task": "0.2.1"

some code:

file battery
export const getLevel = onLevel => {
  DeviceBattery.getBatteryLevel().then(level => {
    const battery_level = Math.round(level * 100);
    onLevel(battery_level);
  });
}

file in use
import * as Battery from '../battery';

try {
        DeviceBattery.getBatteryLevel().then(level => {
            const battery_l = Math.round(level * 100);
        });
 } catch (e) {
        console.log("error", e);
}

Shelestenko avatar Apr 12 '18 11:04 Shelestenko

I had to fork it to add this. My fork hasn't been updated (I no longer use it in the background, as it was unreliable), but the commit in question is here: https://github.com/ascorbic/react-native-device-battery/commit/6d79ee319b151c8f46b7491f2b75e5c863451d43

ascorbic avatar Apr 12 '18 11:04 ascorbic

@ascorbic It seems i cannot checkout to you commit (Maybe it was deleted. Error "Reference is not a tree"). So do you have better ideas what module to use to impement that feature - check battery level? Or maybe i need to duplicate your commit to have reference to checkout?

Shelestenko avatar Apr 12 '18 14:04 Shelestenko

You can’t checkout unless you’ve added mine as an upstream and git fetched it. It’s a single line commented out though, so you may as well do it yourself.

ascorbic avatar Apr 12 '18 14:04 ascorbic

Thanks for opening this issue.

Do we have any folks in this thread that are experienced with Android? I see in https://github.com/ascorbic/react-native-device-battery/commit/6d79ee319b151c8f46b7491f2b75e5c863451d43 you removed maybeUnregisterReceiver in onHostPause -- are there any repercussions in keeping a BroadcastReceiver active while the app is in the background?

I assume not -- I put that in place because I saw a lot of prior art doing this. Happy to remove that if there's no consequences with keeping listeners active while the app is backgrounded.

atticoos avatar Apr 12 '18 15:04 atticoos

cc @christophermark

atticoos avatar Apr 13 '18 18:04 atticoos

@ascorbic Yea. any bad effects if we remove maybeUnregisterReceiver in onHostPause ? You mentioned that its unreliable. In what way if I may ask?

iadcg avatar Dec 21 '18 01:12 iadcg

@iadcg It didn't give the correct result if the app hadn't been launched since switching on. My use case was to detect if the device was on charge when running a background task, as it was running on single-use devices that were only supposed to sync while charging.

ascorbic avatar Dec 21 '18 08:12 ascorbic

I could see this as being configurable by the developer - we could provide a static setter which can be manually changed by developers who want to enable the library in the background:

private static boolean runInBackground = false;

public static void setRunInBackground(boolean runInBackground) {
    this.runInBackground = runInBackground;
}

And then we could check for this value before the stop/resume lifecycle override methods.

Ultimately I guess it's up to the developer to choose whether they want this in the background. I was looking through recent API changes, and they heavily discourage background use of broadcasts, but to each their own! https://developer.android.com/guide/components/broadcasts

Seems like we might want to keep the default as not running in the background, to avoid a queue of messages stacking up for the next time the app opens.

christophermark avatar Dec 21 '18 20:12 christophermark