Android后台服务停止处理广播事件

发布于 2024-10-01 15:38:15 字数 295 浏览 4 评论 0原文

我的 Android 应用程序有问题。 我正在使用后台服务(状态栏上有通知),该服务每小时执行一次音频样本。为了执行此示例,我每小时生成一个广播事件,当我捕获该事件时,录音开始。白天,当我使用手机时,没有任何问题。这种奇怪的行为发生在夜间,此时手机似乎进入“空闲”状态:广播事件不再被处理。从日志文件中,我了解到事件是生成的并放入队列中......当我再次开始使用手机时,所有这些事件都会在同一时刻引发。 你知道如何解决这个问题吗?如何强制手机继续处理我的事件?我是否必须设置任何标志变量以避免手机进入这种“空闲”状态? 希望有人能帮助我!

先感谢您! 米凯拉

I've a problem with my android application.
I'm using a background service (with notification on the status bar) which performs an audio sample every hour. To perform this sample I generate a broadcast event every hour and when I capture the event the audio recording starts. During the day, when I'm using the phone, I've no problems. The strange behavior happens during night, when it seems like if the phone goes to an "idle" state: the broadcast events are no more processed. From the log file I understood that the events are generated and putted in a queue... and when I start using again the phone, all those events are raised in the same moment.
Do u have any idea about how to solve this problem? How can I force the phone to continue processing my events? do I have to set any variable of flag to avoid the phone going in this kind of "idle" state?
hope someone can help me!

thank you in advance!
michela

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

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

发布评论

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

评论(1

青衫负雪 2024-10-08 15:38:16

我正在使用后台服务(状态栏上有通知),该服务每小时执行一次音频样本。

为什么不使用 AlarmManager,这样就不会占用太多内存?用户确实不喜欢永远的服务。

奇怪的行为发生在夜间,此时手机似乎进入“空闲”状态:广播事件不再被处理。

当然。

你知道如何解决这个问题吗?

使用 AlarmManagerWakefulIntentService。安排 AlarmManager 每小时调用您的应用程序(最好通过 setInexactRepeating())。让 WakefulIntentService 获取您的示例。如果您遵循记录的 WakefulIntentService 配方,设备将保持清醒状态足够长的时间,以便您获取样本,然后重新进入睡眠状态。您的服务不会一直保留在内存中。您获得了功能,用户获得了更好的设备性能。

不要只使用WakeLock,这样您的应用程序不仅会一直消耗内存,而且会一直保持 CPU 运行。

I'm using a background service (with notification on the status bar) which performs an audio sample every hour.

Why not use AlarmManager, so you do not take up as much memory? Users really do not like everlasting services.

The strange behavior happens during night, when it seems like if the phone goes to an "idle" state: the broadcast events are no more processed.

Of course.

Do u have any idea about how to solve this problem?

Use AlarmManager and a WakefulIntentService. Schedule the AlarmManager to invoke your application every hour (ideally via setInexactRepeating()). Have the WakefulIntentService obtain your sample. If you follow the documented WakefulIntentService recipe, the device will stay awake long enough for you to get your sample, then will fall back asleep. Your service will not remain in memory all of the time. You get your functionality, and the user gets better device performance.

Do NOT just use a WakeLock so your application not only eats up memory all of the time, but keeps the CPU running all of the time.

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