如何更新一个部件,如果相关服务就会被杀死? [英] How to update a widget if the related service gets killed?

查看:193
本文介绍了如何更新一个部件,如果相关服务就会被杀死?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个录音程序,为此,我目前正在开发一个小部件。

I have an audio recording app, for which I am currently developing a widget.

录制由音频引擎在运转摆在前台状态下的服务执行。

Recording is performed by an audio engine running in a service put in the foreground state.

每当音频引擎状态更改为暂停/播放/录制,广播发送,并通过接收器更新微件办理。

Whenever the audio engine state changes to pause/play/record, a broadcast is sent, and handled by a receiver which updates the widget.

这样在点击了插件的录音键开始录音,这将导致广播发送,并在年底的录制按钮变成红色的小部件。同样的事情发生,如果你开始从应用程序,而不是部件中记录。

So that clicking on the record button in the widget starts recording, which causes the broadcast to be sent, and in the end the record button to turn red in the widget. Same thing happens if you start recording from within the app instead of the widget.

一切的好为止。

现在,如果该服务被杀害出于某种原因,的onDestroy()将不会调用,我没有这样的记录按钮关闭发送暂停或关机广播的机会。结果:该按钮一直显示为红色,而拍摄暂停

Now, if the service gets killed for some reason, onDestroy() is not called and I don't have a chance to send a pause or shutdown broadcast so that the record button turns off. Result: the button stays red whereas recording has stopped.

这是一些可能发生的最糟糕的事情。

为什么呢?用户需要看看他的主屏幕,他/她看到的红色按钮,认为仍在进行录制。

Why? The user takes a look at his home screen, he/she sees the red button and thinks recording is still being performed.

它不喜欢的音乐播放,在这里用户可以注意到,当音乐停止时通过耳机播放...随着录音,你没有听到什么,如果它停止。

It's not like with music playback, where the user can notice when music stops playing through the headphones... With recording, you don't hear nothing if it stops.

我可以理解,需要系统杀死服务。我很好的。我不需要的服务得到重新启动,所有这一切。

I can understand the need for the system to kill services. I'm fine with that. I don't need the service to get restarted and all that.

但我需要更新部件,如果该服务关闭,使用户不被误导,以为他的采访/演唱会/彩排/备忘录仍然被记录下来,而事实并非如此。

But I need to update the widget if the service shuts down, so that the user is not misled, thinking that his interview/concert/rehearsal/memo is still being recorded, while it's not.

所以,我怎么能更新部件的快速的,如果该服务被杀死?

So how can I update the widget quickly if the service gets killed?

推荐答案

我已经找到了我认为是一个答案,以我自己的问题。

I have found what I consider to be an answer to my own question.

我返回 START_STICKY onStartCommand ()在我的服务。

I return START_STICKY from onStartCommand() in my service.

如果我手动从杀DDMS的服务,我得到这个logcat的:

If I manually kill the service from DDMS, I get this in logcat:

I/ActivityManager( 2500): Process com.foo.bar (pid 21874) has died.
W/ActivityManager( 2500): Scheduling restart of crashed service com.foo.bar/.FooBarService in 5000ms

和几秒钟后:

I/ActivityManager( 2500): Start proc com.foo.bar for service com.foo.bar/.FooBarService: pid=22036 uid=10107 gids={1015, 3003} 

这项服务确实是重新启动,并允许我发送从 onStartCommand广播(),以表明音频引擎暂停(我不自动恢复录音)

The service does restart, and that allows me to send a broadcast from onStartCommand() to indicate that the audio engine is paused (I don't resume recording automatically).

小部件提供商应对这种广播和适当地更新微件。

The widget provider reacts to this broadcast and updates the widget appropriately.

这适用于升级Froyo,解决了我的问题:用户不是误导时,看着小部件。录制已停止,因为发动机坠毁,和小部件反映了这一点。

This works on Froyo and solves my problem: the user is not misled when looking at the widget. Recording stopped because the engine crashed, and the widget reflects this.

这不是防弹的,因为它依靠系统自动重新启动该服务,但我认为这是相当安全的。

It's not bullet-proof, as it relies on the system restarting the service automatically, but I think that's quite safe.

这篇关于如何更新一个部件,如果相关服务就会被杀死?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆