螺纹或服务 [英] Thread or services

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

问题描述

我很困惑线程和服务之间的机器人。 如果我要下载一些文件形式的服务器。它可能是在一个时间多个文件。 我应该在这种情况下的线程或服务的选择呢?

I'm confused between thread and service on android. If I have to download some file form server. It may be multiple files at a time. What i should choose in this situation thread or services?

推荐答案

一>不让它在后台运行。你必须在你的服务中创建一个线程,以及等待下载完成。

Doing Stuff in a Service does not make it run in the background. You have to create a Thread in your service as well to wait for the download to complete.

如果您正在使用一个线程,是当地的一个活动,您将有问题,离开该活动,发送到后台或中断来电等,因为这可能会导致您的应用程序的操作系统被打死以释放内存。在这种情况下,你的线程将丢失,可能不会被重新启动,使您的应用程序崩溃。

If you are using a thread that is local to an activity you will have problems with leaving the activity, sending it to the background or an interruption because of an incoming call etc. This may cause your app to be killed by the OS to free up memory. In this case your thread is lost and may not get restarted and make your app crash.

如果你创建你的不作为可能被杀害,不只是一个线程服务,您可以指定操作系统重新启动该服务后,它被杀害的操作系统。这使得服务很长时间下载一个更安全的选择。我使用的服务在我的应用程序之一,以下载一个20 MB的文件,并在该服务中创建一个线程,做下载。

If you create a service you are not as likely to be killed then just a thread and you can specify the OS to restart the service after it gets killed by the OS. This makes the service a more secure choice for very long downloads. I use a service in one of my apps to download a 20 mb file and in this service create a thread that does the downloading.

一个服务,不仅可以帮助你来封装下载,并从活动脱钩,并为您提供了将prevent的线程就此消失,因为低内存操作系统另一种状态。

A service only helps you to encapsulate the download and decouples it from the activity and gives you another state in the OS that will prevent the thread from just disappear because of low memory.

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

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