屏幕超时时是否会调用 Activity onPause 方法?
我无法理解屏幕超时功能以及它将如何影响我的活动中运行的代码。当屏幕超时时 onPause() 会被调用吗?
我有一个活动和一个相关服务正在运行 AsyncTask,它从 60 秒开始倒计时。我希望能够锁定屏幕,并在计时器结束时启动一项新服务和新活动,以发出警报并振动手机。当用户唤醒手机时,他们应该会看到新的活动,其显示正在通过新服务的广播接收器进行更新。
我发现这种行为是高度不可预测的。一旦屏幕超时,它通常会发出警报并在锁屏下显示新的活动,但这需要 2-4 分钟,有时根本不会发生。我似乎手动锁定屏幕可以获得更好的结果,而不是等待超时,但它仍然是不可预测的,并且每个手机都有所不同。
如果有人对幕后发生的事情有任何想法/建议,甚至这种方法是否明智,我们将不胜感激。
问候
I'm having trouble understanding the screen timeout function and how that will effect code running in my activities. Does onPause() get called when the screen times out?
I have an activity and a related service running an AsyncTask, which is counting down from 60 seconds. I want to be able to lock the screen, and at the end of the timer, start a new service and new activity that sounds an alarm and vibrates the handset. When the user wakes the phone they should see the new activity, whose display is being updated via a broadcast receiver from the new service.
What I am finding is that the behaviour is highly unpredictable. Once the screen times out, it will usually sound the alarm and bring up the new activity under the lockscreen, but this takes anywhere between 2-4 minutes, and sometimes doesn't happen at all. I seem the get better results locking the screen manually, rather than waiting for a time-out, but it's still unpredictable and varies per handset.
If anyone had any thoughts/suggestions as to whats going on under the hood, and even whether this approach is sensible, the would be much appreciated.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我找到了答案,我认为我的问题一开始就有点复杂,所以我将尝试回答各个部分,并添加我的实际解决方案......
我有一个
正在启动
。Service
的 ActivityService
(其中包括一个AsyncTask
)应该在后台运行,监视加速度计并为计时器倒计时。然后,Service
定期向Activity
发送广播Intent
,当广播包含某些数据时,Activity
需要运行一些代码。这一切都需要在屏幕关闭的情况下发生。我的发现是,是的,当点击屏幕锁定时,
Activity
确实运行onPause()
,但仍然完全能够在内部BroadcastReciever 中执行代码
类,因此可以调用活动主流程中的方法。我认为这是一种糟糕的做事方式,但它确实有效。问题出在
AsyncTask
和Service
上。无论它的设置方式如何,(startForegrond()
等)当 cpu 关闭时(通过按锁定键,或等待屏幕关闭),后台线程都会暂停一段时间。暂停)。我发现它暂停了大约1分钟的时间间隔。答案是使用
PowerManager.WakeLock
并将标志设置为PARTIAL_WAKE_LOCK
。这将允许屏幕关闭,但保持线程在后台全速运行。文档在这里: http://developer.android.com/reference/android/os/ PowerManager.html
完成后不要忘记释放它,因为它对电池的消耗很大。
Ok, so I've found the answer, I think my question was a little convoluted to begin with, so I'll try to answer the various parts, as well as adding my actual solution....
I had an
Activity
that was starting aService
. TheService
(which included anAsyncTask
) was supposed to run in the background, monitoring the accelerometer and counting down a timer. TheService
was then periodically sending a broadcastIntent
to theActivity
, when the broadcast contained certain data, theActivity
needed to run some code. This all needed to happen with the screen off.My findings are that, yes, when hitting the screen lock, the
Activity
does runonPause()
, but is still fully able to execute code in the internalBroadcastReciever
class, and consequently this can call methods in the activities main process. I imagine this is a bad way to do things, but it did work.The issue was with the
AsyncTask
and theService
. Regardless of the the way it's set up, (startForegrond()
etc ) a background thread will be paused for a while when the cpu shuts down (either by pressing the lock key, or waiting for the screen to time out). I found it was paused for intervals of about 1 min.The answer is to use
PowerManager.WakeLock
and set the flag toPARTIAL_WAKE_LOCK
. This will allow the screen to shut down but keep threads running at full speed in the background.Doc here: http://developer.android.com/reference/android/os/PowerManager.html
dont forget to release it when done as it is expensive on the battery.