开发者

Stopping a widget's service when not in use

I'm having trouble getting my widget to stop hogging so many resources.

I setRepeating on the AlarmManager to update. When the widget is deleted (开发者_运维百科onDisabled()), I call .cancel() on the service, but the service still shows up in the android "running processes" making me believe I'm doing something wrong. Any guidance?

Also, should I be scheduling my updates in the onEnabled() or the onUpdate()? onUpdate doesn't seem to work on anything other than the first widget.

Thanks for any help you can give!


The best practice thing to do is to stop your service after it has done what it needs to do rather than letting the widget be deleted before you stop it.

You should let it do it's update to the widget then call stopSelf(); If you have an AlarmManager set to start the service again this shouldn't be a problem.

Regarding onUpdate. You have to set the frequency of the widget update in your widgetconfig XML file. As of android 1.6 I believe the shortest possible update is 30 mins. (If you need to do an update more often then alarm manager is your best bet.) onUpdate will pass an array of widget Ids and you need to loop through them to get all of your widgets.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜