应用程序线程VS服务线程 [英] Application threads vs Service threads

查看:115
本文介绍了应用程序线程VS服务线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是放置一个漫长的网络访问code在一个线程中的活动或服务线程的优势/劣势?它如何影响应用程序?我写的流媒体音频播放器,并从我读过到目前为止,把code在服务最终仍要阻止这样一个新的线程需要的应用,没有人知道这是否更有意义,把这种一张code的服务。

What are the advantages/disadvantages in placing a lengthy network access code in a thread in an activity or a thread in a service? How would it affect the application? I am writing a streaming audio player and from what I've read so far putting the code in a service will still end up blocking the application so a new thread is needed, does anyone know if it makes more sense to put this piece of code in a service.

感谢

推荐答案

是的,在服务阻塞操作仍然会阻止该应用程序。尽管第一个出场,服务不仅仅是在后台运行的任务。它们是用于与生命周期独立于活动的生命周期的正在运行的任务(即,他们可以继续后活性被关闭)。

Yes, a blocking operation in a Service will still block the application. Despite first appearances, Services are not simply for running tasks in the background. They are for running tasks with a lifecycle that is independent of the Activity lifecycle (IE, they may continue after the Activity is closed).

在活动开始时,活动结束时开始和结尾的服务是没有用的。

A Service that starts when an Activity starts and ends when the Activity ends is useless.

在你的情况,你在哪里流式音频,你可能要流音频用户关闭活动,在这种情况下,你应该使用一个服务,即使,但你仍然需要一个线程(或AsyncTask的)阻断任务。

In your case, where you are streaming audio, you may want to stream audio even after the user closes the Activity, in which case, you should use a Service, but you'll still need a thread (or an AsyncTask) for blocking tasks.

这篇关于应用程序线程VS服务线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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