我的屏幕保护程序应用程序中出现奇怪的生命周期行为

发布于 2024-11-23 19:01:19 字数 811 浏览 1 评论 0原文

我刚刚开发了一个屏幕保护程序应用程序,我在其生命周期中发现了一个奇怪的行为。 我的工作流程是这样的:

  1. 启动我的RegisterService,我在其中调用registerReceiver方法来注册一个BroadcastReceiver,它可以接收ACTION_SCREEN_OFF< /代码>。

2.在此BroadcastReceiveronReceive方法中,我启动一个活动作为屏幕保护程序。

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:

  1. start my RegisterService, where I call registerReceiver method to register a BroadcastReceiver, which can receive ACTION_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().
enter image description here

Any idea? This problem handicaps me in programming, please help. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2024-11-30 19:01:19

基于对 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...

星星的轨迹 2024-11-30 19:01:19

我可以向您推荐一些可能对您有用的非常简单的方法,如果您不想要暂停行为,为什么不尝试重写方法 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 call super.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文