Android 服务、异步任务和线程之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3264383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Service, Async Task & Thread?
提问by SpunkerBaba
What is the difference between Service, Async Task & Thread. If i am not wrong all of them are used to do some stuff in background. So, how to decide which to use and when?
服务、异步任务和线程之间有什么区别。如果我没有错的话,所有这些都用于在后台做一些事情。那么,如何决定使用哪个以及何时使用?
回答by Pentium10
Probably you already read the documentation description about them, I won't repeat them, instead I will try to give answer with my own words, hope they will help you.
可能你已经阅读了有关它们的文档说明,我不会重复它们,而是尝试用我自己的话来回答,希望它们能帮助你。
Service is like an Activity but has no user interface. Probably if you want to fetch the weather for example you won't create a blank activity for it, for this you will use a Service.
A Thread is a Thread, probably you already know it from other part. You need to know that you cannot update UI from a Thread. You need to use a Handler for this, but read further.
An AsyncTask is an intelligent Thread that is advised to be used. Intelligent as it can help with it's methods, and there are three methods that run on UI thread, which is good to update UI components.
服务就像一个活动,但没有用户界面。例如,如果您想获取天气,则可能不会为其创建空白活动,为此您将使用服务。
一个线程就是一个线程,可能你已经从其他部分知道它了。您需要知道您不能从线程更新 UI。您需要为此使用处理程序,但请进一步阅读。
AsyncTask 是建议使用的智能线程。智能,因为它可以帮助它的方法,并且在 UI 线程上运行三个方法,这很好地更新 UI 组件。
I am using Services, AsyncTasks frequently. Thread less, or not at all, as I can do almost everything with AsyncTask.
我经常使用服务,AsyncTasks。线程更少,或者根本不线程,因为我几乎可以用 AsyncTask 做所有事情。
回答by Alejandro Serret
This is the easiest answer for your question
这是您问题的最简单答案
Thread
线
is an unit of execution who run "parallel" to the Main Threadis an important point, you can't update a UI component from the any thread here except main thread.
是一个与主线程“并行”运行的执行单元是一个重要的点,除了主线程之外,您不能从这里的任何线程更新 UI 组件。
AsyncTask
异步任务
is a special thread, which gives you helper methods to update UIso basically you can update the UI even AsyncTask will run on a background thread. Interprocess communication handling is not required to be done explicitly.
是一个特殊的线程,它为您提供更新 UI 的辅助方法,因此基本上您可以更新 UI,即使 AsyncTask 将在后台线程上运行。不需要显式完成进程间通信处理。
Service
服务
solve the above problem because it live separate from the activity that invoke it so it can continue running even when the activity is destroyed, it run in the Main Thread(beware of ANR) use a background service (extend IntentService it create the worker thread automatically for you). Service is like an activity without UI, is good for long task
解决上述问题,因为它与调用它的活动分开,因此即使活动被销毁它也可以继续运行,它在主线程中运行(注意 ANR)使用后台服务(扩展 IntentService 它自动创建工作线程给你)。服务就像一个没有 UI 的活动,适合长时间的任务
回答by Peter Jankuliak
Few more information I wish someone had told me a few days ago:
我希望有人在几天前告诉我的更多信息:
- You can share global variables - such as threads - between Activities and Services.
- Your applicationtogether with all its global variables will not be wiped out as long as there is an Activity or a Service still present.
- If you have an instance of a Service in your app and the OS needs resources, it first kills your Activities, but as long as there is the Service, the OS won't wipe out your application together with its global variables.
- 您可以在活动和服务之间共享全局变量 - 例如线程。
- 只要活动或服务仍然存在,您的应用程序及其所有全局变量就不会被清除。
- 如果您的应用程序中有一个 Service 实例并且操作系统需要资源,它首先会杀死您的活动,但只要有 Service,操作系统就不会连同其全局变量一起清除您的应用程序。
My use case is like this: I have one thread in global space that is connected to a server and an Activity that shows the results. When user presses the home button, the Activity goes to background and a new Service is started. This service then reads results from the thread and displays information in the notification area when needed. I don't worry about the OS destroying my Activity because I know that as long as the Service is running it won'd destroy the thread.
我的用例是这样的:我在全局空间中有一个线程连接到服务器和一个显示结果的活动。当用户按下主页按钮时,活动进入后台并启动一个新服务。该服务然后从线程读取结果并在需要时在通知区域中显示信息。我不担心操作系统会破坏我的活动,因为我知道只要服务正在运行,它就会破坏线程。
回答by Abbath
In short, Servicefor time consuming tasks
, AsyncTaskfor short-lived tasks
, Threadis a standard java construction
for threads.
总之,Servicefor time consuming tasks
,AsyncTaskfor short-lived tasks
,Thread是一个standard java construction
for 线程。
回答by Rahul Raina
From developer's perspective:
从开发者的角度来看:
Thread: Used to execute the set to codes parallely to the main thread. But you cannot handle the UI inside the thread. For that you need to use Handler
. Hadler works like thread but can handle the UI as well.
线程:用于与主线程并行执行代码集。但是您无法在线程内处理 UI。为此,您需要使用Handler
. Hadler 像线程一样工作,但也可以处理 UI。
ASyncTask: Used for handling those tasks that you cannot make to work on the main thread. For example, an HTTP request is very heavy work that cannot be handeled on the main thread, so you handle the HTTP request in the ASyncTask
It works parallely with your main thread Asynchronously in the background.It has few callback methods that are invoked on their corresponding events.
ASyncTask:用于处理那些不能在主线程上工作的任务。例如,一个 HTTP 请求是非常繁重的工作,无法在主线程上处理,因此您ASyncTask
在后台处理与您的主线程并行工作的 HTTP 请求。事件。
Service: Is a background process. It is employed when you have to do some processing that doesn't have any UI associated with it.
服务:是一个后台进程。当您必须进行一些没有任何关联 UI 的处理时会使用它。
回答by pankaj
service is like activity long time consuming task but Async task allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.
service 就像 Activity 耗时较长的任务,但 Async 任务允许我们执行长时间/后台操作并在 UI 线程上显示其结果,而无需操作线程。