简体   繁体   English

使用WakeLock时,屏幕打开然后关闭的速度过快(几秒钟没有打开)

[英]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. 我在通过PowerManager API使用唤醒锁时遇到问题。 Currently, i have a service and a broadcast receiver. 目前,我有一个服务和一个广播接收器。

My service has 2 main roles : 我的服务有2个主要角色:

  • 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. 第二个,接收器对警报做出反应(在RTC_WAKEUP模式下),并通过打开屏幕来启动服务以播放通知。

I'm using Mark murphy's cwac-wakeful module to achieve that and it works just fine. 我正在使用Mark murphy的cwac-wakeful模块来实现这一点,并且效果很好。

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 调用广播接收器的OnReceive方法
  • It calls WakefulIntentService.sendWakefulWork 它调用WakefulIntentService.sendWakefulWork
  • My wakeLock is acquired (with SCREEN_DIM_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE flags) 我的唤醒锁已获取(带有SCREEN_DIM_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE标志)
  • 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. 我以为使用ON_AFTER_RELEASE(文档说:“如果设置了此标志,则释放WakeLock时将重置用户活动计时器,从而使照明保持更长的时间。如果您需要在唤醒锁定条件之间循环。“)可以解决我的问题,但没有。

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. 在服务完成后不立即释放唤醒锁,而是在接下来的15/20秒(从服务完成时开始)发出另一个警报,该警报将(通过接收器/服务)释放唤醒锁并关闭屏幕。 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 ? 然后我的问题是,如何像接收短信时那样将屏幕保持开启状态几秒钟(例如10/15秒),实现此目的的最佳解决方案是什么?

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. 我遇到类似的情况,因为我释放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 | 我想我对它应用了与您相同的逻辑,但是我将FULL_WAKE_LOCK与其他选项PowerManager.ACQUIRE_CAUSES_WAKEUP |一起使用。 PowerManager.ON_AFTER_RELEASE. PowerManager.ON_AFTER_RELEASE。 This seemed to do the trick for me in my "GameTime" timer application. 在我的“ GameTime”计时器应用程序中,这似乎对我有用。 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." API文档中有一行指出:“此外,您还可以添加两个标志,这些标志仅影响屏幕的行为。与PARTIAL_WAKE_LOCK结合使用时,这些标志无效。” I wonder if SCREEN_DIM_WAKE_LOCK counts as a PARTIAL WAKE LOCK? 我想知道SCREEN_DIM_WAKE_LOCK是否算作部分唤醒? (undocumented 'feature') (未记录的“功能”)

API located here: http://developer.android.com/reference/android/os/PowerManager.html API位于: 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 设置新警报在10/15秒后发生
  3. Release the wake lock when the second alarm is triggered 触发第二个警报时释放唤醒锁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 屏幕关闭时释放WAKELOCK - Release WAKELOCK when screen is off 屏幕关闭时,Android Wakelock无法正常工作 - Android Wakelock not working when screen is off 使用部分唤醒锁关闭屏幕时阻止应用程序进入OnPause - Prevent app from going on OnPause when turning screen off using partial wakelock 使用“ PROXIMITY_SCREEN_OFF_WAKE_LOCK”唤醒锁后,不应该触发的触摸事件UP - Touch event UP being fired when not supposed to, after using “PROXIMITY_SCREEN_OFF_WAKE_LOCK” wakelock WakeLock没有释放且屏幕没有关闭 - WakeLock not releasing and Screen isn't turning off 即使使用WakeLock,当Droid / Nexus One屏幕关闭时,Accelerometer也会停止提供样品 - Accelerometer stops delivering samples when the screen is off on Droid/Nexus One even with a WakeLock WakeLock仅在充电时打开屏幕 - WakeLock turns screen on only when charging 当屏幕关闭时,MediaPlayer会在Lollipop上过早地切断播放 - MediaPlayer cutting off playback too early on Lollipop when Screen is off WakeLock.release()会关闭屏幕,即使用户正在与应用程序进行交互 - WakeLock.release() switches off screen even if the user was interacting with the app 广播接收器是否需要ACTION_SCREEN_OFF唤醒锁 - Is wakelock needed in broadcast receiver for ACTION_SCREEN_OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM