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

Devices don't lock when service is running #157

Closed
insertjokehere opened this issue May 27, 2022 · 16 comments
Closed

Devices don't lock when service is running #157

insertjokehere opened this issue May 27, 2022 · 16 comments

Comments

@insertjokehere
Copy link
Contributor

Thanks for your awesome plugin!

I've implemented a foreground service on Android, but in my testing I have noticed that devices won't lock automatically or even dim their screens when the service is running. It appears that this is caused by this library acquiring a full wake lock:

https://github.com/ekasetiawans/flutter_background_service/blob/master/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/BackgroundService.java#L53-L62

According to the docs, a full wake lock "Ensures that the screen and keyboard backlight are on at full brightness".

It looks like this was introduced in #92 - is it possible to override this behavior?

@ekasetiawans
Copy link
Owner

hi @insertjokehere that seems to be the cause, maybe we can add a Method Channel to release the lock when the flutter engine runs successfully.

@ekasetiawans
Copy link
Owner

@insertjokehere I have published 2.1.1 to fix this issue. Could you confirm if it's solved?

@insertjokehere
Copy link
Contributor Author

insertjokehere commented May 27, 2022 via email

@insertjokehere
Copy link
Contributor Author

Hi,

Looking at the fix c083025 it seems like the library still acquires a full wake lock when the background service starts, but releases it after 10 minutes, or when the main application interacts with the service.

A couple of questions:

  1. Does this mean that if the the service is set to start automatically at boot, when the phone turns on screen and CPU sleeping will be disabled for 10 minutes unless the user opens the app?
  2. Why does this library need a full wake lock in the first place? @mohammedhasna2015 - what issue were you fixing with Fix run service background when charger not connected and screen lock  #92?

@ekasetiawans
Copy link
Owner

@insertjokehere I added the timeout just for backup. The wakelock will be released when the flutter engine is started. You can find the release command here https://github.com/ekasetiawans/flutter_background_service/blob/master/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/BackgroundService.java at line 253

@insertjokehere
Copy link
Contributor Author

The wakelock will be released when the flutter engine is started

Thats great, thanks for clarifying.

I guess my question is more why does the library need a full wake lock in the first place? Full wake lock is a really extreme lock to acquire - it turns the screen on full brightness, stops it from turning off at all and locks the CPU into full power mode. Before #92, the library acquired a partial wake lock that keeps the CPU on but does let it throttle down if needed, why was that insufficient? Would partial wake lock + the new timeout logic work? Or is there some reason why a full wake lock is needed in order to make sure the service starts properly?

@ekasetiawans
Copy link
Owner

@insertjokehere afaik, we need wake lock for making boot receiver works. If you have any other strategy, feel free to make a pull request. :)

@insertjokehere
Copy link
Contributor Author

Ah, makes sense. Thanks!

@intenseandnoble
Copy link

This problem seems to also exist in iOS. I tested on an iPhone with the latest version of this package. Swift not being my cup of tea, even after reading the code, I am not certain why.

@ekasetiawans
Copy link
Owner

@iovasquez That's weird, we don't do a wakelock like we have done on Android.

@intenseandnoble
Copy link

intenseandnoble commented Jun 8, 2022

Yes, after talking with my team: it was a miscommunication of which platform was being tested.

(while I did test on iOS also, the issue I had had another cause)

Thank you for your quick answer!

@Grand-87
Copy link

Grand-87 commented Jun 8, 2022

Hi, first of all - thanks for great plugin:)
I faced the same issue with locking so I tested your fix, unfortunately it's still doesn't solve the issue for me.
I download source codes and run a few tests.
What I found is:

  1. when I commented out wakelock acquiring (getLock(getApplicationContext()).acquire(10*60*1000L /*10 minutes*/)) - device is locking as as expected.
  2. What is strange for me - after adding few logs i found out that
    -wakelock is acquiring correctly
    -lockStatic.isHeld() after acquiring is set to true
    -after lockStatic.release() calling lockStatic.isHeld() returns false.
    so everything looks all right but.. device isn't locking and screen remains turned on.
    I'm not a java/android programmer so it is hard for me to say what is wrong so I want to ask is there anything You see that could causing this issue?

Only solution I come out with is - if

wake lock is required only for making boot receiver works

then maybe it should depend on auto_start_on_boot variable? When it is false we don't need to lock.
That would fix my problem because I'am not using autostart, but it's more of a workaround then a real fix for this issue.

I ran tests on:
-Samsung Galaxy S10 | Android 12
-Huawei Mate 20 lite | Android 10
-Blackberry Key2 | Android 8.1

[√] Flutter (Channel stable, 3.0.1, on Microsoft Windows [Version 10.0.19044.1706], locale pl-PL)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)

@ekasetiawans ekasetiawans reopened this Jun 9, 2022
@jakeump
Copy link

jakeump commented Jun 14, 2022

Hi @ekasetiawans
I was glad to see this is an open issue. I am having the same issue; I have the wakelock plugin, and I have that tied to a setting to "keep screen on." Before I started using this package (it's fantastic btw), it worked perfectly. I'm finding that after I integrated background service into my project, the screen is always staying on and not even dimming. I'm running 2.1.2 and testing with Android 12

@ekasetiawans
Copy link
Owner

ekasetiawans commented Jun 14, 2022

I just released flutter_background_service:^2.1.3 to fix this issue, please reply this issue if the problem persists and i will reopen it.

@Grand-87
Copy link

I have tested your fix on

-Samsung Galaxy S10 | Android 12
-Huawei Mate 20 lite | Android 10
-Blackberry Key2 | Android 8.1

and everything works great, thanks for your work!

I'm curious, can you tell in a few words what was the problem?

@ekasetiawans
Copy link
Owner

@Grand-87 the wake lock acquired multiple times by the watchdog receiver but only released once. So, I move the acquire command to the correct location to make sure it's called once.

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