与任务队列的Andr​​oid后台服务

编程入门 行业动态 更新时间:2024-10-27 14:24:16
本文介绍了与任务队列的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

推荐答案

希望这架构将帮助您:

  • 如果您要执行您的活动结束后,即使以后再使用你的任务 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后台服务

    本文发布于:2023-11-27 05:01:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1636704.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:队列   后台   Andr   oid

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!