我的屏幕保护程序应用程序中出现奇怪的生命周期行为
我刚刚开发了一个屏幕保护程序应用程序,我在其生命周期中发现了一个奇怪的行为。 我的工作流程是这样的:
- 启动我的
RegisterService
,我在其中调用registerReceiver
方法来注册一个BroadcastReceiver
,它可以接收ACTION_SCREEN_OFF< /代码>。
2.在此BroadcastReceiver
的onReceive
方法中,我启动一个活动作为屏幕保护程序。
3.在活动中,我编写Log.i()
语句来跟踪其运行情况。
我的问题是:
当屏幕超时,或者当我按下POWER键时,屏幕关闭,系统将发送ACTION_SCREEN_OFF消息。正如我所料,我的接收器启动了屏幕保护程序活动。但是,我发现此 Activity 调用 onCreate()
、onResume()
、onPause()
、onResume()
根据logcat中的输出顺序。
似乎某个活动出现在我的屏幕保护程序前面并立即完成,因此我的屏幕保护程序调用 onPause()
然后 onResume()
。
有什么想法吗?这个问题对我的编程造成了阻碍,请帮忙。谢谢!
I just developed a screen saver app and I found a strange behavior in its lifecycle.
My work flow is like this:
- start my
RegisterService
, where I callregisterReceiver
method to register aBroadcastReceiver
, which can receiveACTION_SCREEN_OFF
.
2.In the onReceive
method of this BroadcastReceiver
, I start an activity as the screensaver.
3.In the activity, I write Log.i()
statement to track its running.
My question is:
When the screen times out, or when I press the POWER key, the screen turns off, and the system will send ACTION_SCREEN_OFF message. As I expect, my receiver starts the screen saver activity. However, I find this Activity calls onCreate()
, onResume()
, onPause()
, onResume()
sequentially according to the output in logcat.
It seems as if some a activity comes at front of my screensaver and finishes immediately, so my screensaver calls onPause()
and then onResume()
.
Any idea? This problem handicaps me in programming, please help. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于对 PowerManagerService.java 源代码,当需要关闭屏幕时,系统会启动一个动画(请查看类源代码的第 2183 行)来执行此操作。这意味着您的活动将暂停,然后在动画结束后恢复。
我不能 100% 确定这一点,因为我还没有在我的环境中测试过它,但这是我为您的情况找到的唯一合乎逻辑的解释。
希望这有帮助...
Well based on a brief study of the PowerManagerService.java source code, when it's time to turn the screen off, the system initiates an animation (look at line 2183 for the class source) to do that. That means that your activity will pause and then will resume after the animation has ended.
I cannot be 100% sure for this, since I haven't tested it in my environment but this is the only logical explanation I found for your situation.
Hope this helps...
我可以向您推荐一些可能对您有用的非常简单的方法,如果您不想要暂停行为,为什么不尝试重写方法
onPause()
并且什么都不做:P 不要调用 super.onPause() ,这将终止它的行为。其他可能对您有用的事情是,声明一个静态变量,在“onResume()”方法上添加 1,并在调用“onStop()”时返回“0”。现在只需评估何时调用“onResume()”,如果变量为“0”,则为第一次,其他任何操作都不执行。
我希望这会有所帮助,因为关于您的问题没有太多更具体的信息。
I can recommend you something very easy that might work for you, if you do not want the pause behavior why don't you try to Override the method
onPause()
and just do nothing :P don't callsuper.onPause()
and that will terminate the behavior of it.Other thing that might work for you, declare a static variable, add 1 on the "onResume()" method and return to "0" when "onStop()" is called. now just evaluate when the "onResume()" is called and if the variable is "0" then is the first time, anything else do nothing.
I hope this helps cause there is no much information on your question to be more specific.