gpt4 book ai didi

android - 如何让 Android 操作系统知道我的应用程序在后台时有事情要做?

转载 作者:行者123 更新时间:2023-11-29 02:10:08 26 4
gpt4 key购买 nike

我正在设计一个 android 应用程序,它需要在处于 Activity 状态时每分钟播放一次声音。

我的逻辑是使用可运行对象和处理程序在 Activity 状态下每分钟播放一次声音。如果您按下开始或停止按钮,该应用程序可以正常运行。

如果您运行该应用程序并点击停止,它会终止该应用程序。如果您运行该应用程序,声音会继续播放,并且当您重新启动时,计时器将是最新的。这是预期的。

我遇到的问题是,如果你回到家然后启动另一个应用程序,我的应用程序很有可能被杀死并且声音将不再播放(因此当你重新启动时 GUI 将被重置)

我的问题是,我应该使用什么方法让我的应用程序继续播放声音,直到我告诉它停止,即使它不在前台?声音可以被认为是作为对用户的提醒。

我曾尝试使用谷歌机器来帮助我,但我没有运气。

最佳答案

If you run the app hit home the sounds continue to play and when you relaunch the timer will be up to date. This is expected.

除了它不会持续。

The problem i am having is that if you hit home and then launch another app, there is a high chance my app gets killed and the sounds will no longer play (and consequently GUI will is reset when you relaunch)

正确。

What approach should i use so that my app will continue to play the sounds until i tell it to stop, even if its not in the foreground?

A service .在一分钟的时间内,您处于我认为您应该使用 AlarmManagerIntentService 的位置与您现有的 Handler方法可行。

您应该将 startForeground() 与该服务一起使用,既可以防止 Android 将其关闭,也可以为用户提供一种非常简单的方法来返回 Activity 以关闭该服务(通过您通过 startForeground() 建立的 Notification

关于android - 如何让 Android 操作系统知道我的应用程序在后台时有事情要做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com