Android后台服务停止处理广播事件
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用
AlarmManager
,这样就不会占用太多内存?用户确实不喜欢永远的服务。当然。
使用
AlarmManager
和WakefulIntentService
。安排AlarmManager
每小时调用您的应用程序(最好通过setInexactRepeating()
)。让WakefulIntentService
获取您的示例。如果您遵循记录的WakefulIntentService
配方,设备将保持清醒状态足够长的时间,以便您获取样本,然后重新进入睡眠状态。您的服务不会一直保留在内存中。您获得了功能,用户获得了更好的设备性能。不要只使用
WakeLock
,这样您的应用程序不仅会一直消耗内存,而且会一直保持 CPU 运行。Why not use
AlarmManager
, so you do not take up as much memory? Users really do not like everlasting services.Of course.
Use
AlarmManager
and aWakefulIntentService
. Schedule theAlarmManager
to invoke your application every hour (ideally viasetInexactRepeating()
). Have theWakefulIntentService
obtain your sample. If you follow the documentedWakefulIntentService
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.