简体   繁体   中英

Screen on and then off too fast when using WakeLock (not remaining on for a few second)

I'm facing a problem using Wake lock through PowerManager API. Currently, i have a service and a broadcast receiver.

My service has 2 main roles :

  • First one, it creates an alarm at a specific time (my receiver receives the boot completed intent from system and start the service to create the next alarm)
  • Second one, the receiver reacts to the alarm (in RTC_WAKEUP mode) and start the service to play a notification by turning on the screen.

I'm using Mark murphy's cwac-wakeful module to achieve that and it works just fine.

The problem is : the screen turns on and off too fast for the user to see it.

Here is what's happening when the alarm intent is launched (for the second role/case) :

  • OnReceive method of Broadcast receiver is called
  • It calls WakefulIntentService.sendWakefulWork
  • My wakeLock is acquired (with SCREEN_DIM_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE flags)
  • Screen is turned on
  • The service is launched : The notifications is created and displayed
  • Service finishes
  • Wake lock is released
  • Screen is turned off immediatly (not on after release for a few seconds)

As you have probably understood. The problem is, the lock is released too fast after the service finishes so i can just see the screen turning on and off in less that one second. I thought that using the ON_AFTER_RELEASE (the doc says : "If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions. ") would have solved my problem, but no.

When googling, i found a solution that consists in :

  • Not releasing the wake lock just after the service finishes but launching another alarm in the next 15/20sec (from when service finishes) that would release the wakelock (via receiver/service) and turn off the screen. I don't know if it'a good solution and it seems complicated to me for this use case

Then my question is how can i maintain the screen on for a few seconds (say 10/15 sec) like when i receive an sms, what is the best solution to achieve that ?

Any help would be greatly appreciated, and thanks in advance !

I had a similar situation in that my app was shutting down immediately after I released a WakeLock. I think I applied the same logic to it that you did, but I used a FULL_WAKE_LOCK with the other options PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE. This seemed to do the trick for me in my "GameTime" timer application. I know this was posted a while a ago, but maybe it would provide some help to someone!

There is a line in API documentation that states: "In addition, you can add two more flags, which affect behavior of the screen only. These flags have no effect when combined with a PARTIAL_WAKE_LOCK." I wonder if SCREEN_DIM_WAKE_LOCK counts as a PARTIAL WAKE LOCK? (undocumented 'feature')

API located here: http://developer.android.com/reference/android/os/PowerManager.html

I think you provide the answer:

  1. Don't release the wake lock
  2. Set a new alarm to occur 10/15 seconds later
  3. Release the wake lock when the second alarm is triggered

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM