与任务队列的Andr​​oid后台服务 [英] Android Background Service with Task Queue

查看:104
本文介绍了与任务队列的Andr​​oid后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要一个后台服务,在那里我可以提交任务。任务可以是相当通用的,它可能涉及下载资讯,获取位置的一个时间,或者取resources.This服务需要从活动最少的交互并且应当启动的应用程序启动时,如果没有待处理的任务在应该退出队列中。

My app needs a background service, where I can submit tasks to. The tasks can be quite generic, it might involve downloading feeds, fetching location one time, or fetching resources.This service needs minimal interaction from the activities and it should be started when the app launches and should quit if there is no pending task in the queue.

这是个好主意吗?我将如何设计这样的服务?所有建议,欢迎

Is it a good idea? How would I design such a service? All suggestions are welcome

推荐答案

希望这架构将帮助您:


  1. 如果您要执行您的活动结束后,即使以后再使用你的任务 startService(),如果你想尽快的活动终止所有的任务其他饰面然后用 bindService()

当你调用 startservice()其onCreate()方法是不是叫如果服务已在运行,但其 onStartCommand() 被调用每次调用时间 startservice()。你可以使用这个属性。

When you call startservice() its oncreate() method is not called if the service is already running but its onStartCommand() gets called every time you call startservice(). You can use this property.

您可以实现,可以从您的活动进行访问把任务中有一个静态方法,当你在静态方法调用就可以启动一个线程执行任务。

You can implement a static method which can be accessed from your activity to put task in it and when you get the call in static method you can start a thread executing your tasks.

请注意:不要忘记释放所有资源(线程等)服务由于Android不会为你做

Note: Don't forget to release all the resources(threads etc.) in the service as android won't do it for you.

如果你的活动已经完成,然后在任务执行完成,你可以使用 stopSelf()的服务,并释放所有的资源。

If your activity has finished then on task execution complete you can use stopSelf() for the service and release all the resources.

您可以尝试IntentService是否符合您的需求。检查了解更多详情。

you can try IntentService if it meets your need. Check this for more details.

这篇关于与任务队列的Andr​​oid后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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