Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Battery doest not work when app in background #21

Open
Shelestenko opened this issue Apr 12, 2018 · 8 comments
Open

Battery doest not work when app in background #21

Shelestenko opened this issue Apr 12, 2018 · 8 comments

Comments

@Shelestenko
Copy link

Shelestenko commented Apr 12, 2018

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);
}
@ascorbic
Copy link

ascorbic commented Apr 12, 2018

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: ascorbic@6d79ee3

@Shelestenko
Copy link
Author

@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?

@ascorbic
Copy link

ascorbic commented Apr 12, 2018 via email

@atticoos
Copy link
Contributor

atticoos commented Apr 12, 2018

Thanks for opening this issue.

Do we have any folks in this thread that are experienced with Android? I see in ascorbic@6d79ee3 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
Copy link
Contributor

cc @christophermark

@iadcg
Copy link

iadcg commented Dec 21, 2018

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

@ascorbic
Copy link

@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.

@christophermark
Copy link
Contributor

christophermark commented Dec 21, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants