如何在唤醒时处理 Android 活动生命周期(闹钟应用)

发布于 2024-09-26 07:20:34 字数 304 浏览 8 评论 0原文

我正在开发的警报应用程序遇到了一些问题。

我认为有点奇怪的第一件事是当闹钟响起并唤醒手机时。这些事情发生了。

创建时
恢复
暂停
恢复时

为什么它们按这个顺序运行?最后两个不应该被调用吗?这就是给我带来大麻烦的原因,因为当我按 home 或返回电话时, onPause 就会运行,我想从中调用 finish() 。该部分按其应有的方式工作,但是当手机因 onPause 调用而从睡眠状态唤醒时,该部分不起作用......

有什么想法吗?

I'm having a couple of problems with an alarm app I am developing.

The first thing that I think is a bit weird is that when an alarm goes of and wakes the phone up. These things happend.

oncreate
onresume
onpause
onresume

Why are they run in that order? The last two should not be called? And this is what's causes me big trouble, because when i press home or back on the phone, onPause is run, which I want to call finish() from. And that part works as it should, but that does not work when the phone wakes upp from sleep bacause of the onPause call...

Any ideas?

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

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

发布评论

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

评论(3

忘羡 2024-10-03 07:20:34

我也有类似的问题:我在后台创建一个活动和一个服务。当服务从网络接收到一些数据时,它将启动该活动。
一切正常,直到活动进入 onStop 阶段并且屏幕变黑。
当我遇到这种情况并请求重新启动活动时,我有:

onRestart
启动时
恢复时
暂停时
新意图
onResume

如您所见,我有一个 onResume、onPause 和另一个 onResume,并且该活动已发送给用户。

似乎是因为我们使用了 singleTop 或 singleInstance Activity(可能也出现在 singleTask 上)。问题是,当您将 Intent 发送到已创建的 singleTop Activity 时,您的新 Intent 会激活 onNewIntent() 方法。从API中可以看到:

活动将始终暂停
在收到新意图之前,您
可以依赖 onResume() 被调用
使用此方法后。

所以也许这就是您收到 onResume-onPause-onResume 调用的原因。

然而,这是一个奇怪的行为!

I also have a similar problem: i create an Activity and a Service in the background. When the service receive some data from the net it will bring-up the activity.
Everything works fine until the activity in in the onStop phase and the screen goes black.
When I have this situation and I request the activity to restart, I have:

onRestart
onStart
onResume
onPause
onNewIntent
onResume

As you can see I have an onResume, onPause and another onResume and the activity came to the user.

Seems that is because we use a singleTop or singleInstance Activity (maybe also appear on singleTask). The problem is that when you send an Intent to an already created singleTop Activity, your new intent active the onNewIntent() method. from the API you can see that:

An activity will always be paused
before receiving a new intent, so you
can count on onResume() being called
after this method.

so maybe this is why you receive onResume-onPause-onResume calls.

However this is a strange behavior!

2024-10-03 07:20:34

我建议您看看官方警报应用程序如何处理事件:
https://android.googlesource.com/platform/packages/apps/DeskClock

I would suggest you look at how the official alarm application handles the events:
https://android.googlesource.com/platform/packages/apps/DeskClock

素染倾城色 2024-10-03 07:20:34

你真的想在 onPause() 中调用 finish() 吗? onPause() 当当前 Activity 不再处于前台时调用。也许您想在 onStop() 方法中调用 finish() 。有关详细信息,请参阅活动生命周期

Do you really want to call finish() in onPause()? onPause() is called when the current activity is not in the foreground any more. Maybe you want to call finish() in your onStop() method instead. See the Activity lifecycle for details.

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