java 什么时候在 Android 中使用线程/服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7933178/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:04:13  来源:igfitidea点击:

When to use a thread / service in Android?

javaandroidmultithreadingservice

提问by Cody

When should a thread or a service be used?

什么时候应该使用线程或服务?

Should they be used for authentication? For instance, in my app I was considering using a thread or service (I am authenticating via Active Directory.)

它们应该用于身份验证吗?例如,在我的应用程序中,我正在考虑使用线程或服务(我正在通过 Active Directory 进行身份验证。)

Do you have examples of when each would be used?

你有什么时候使用每个的例子吗?

采纳答案by Patrick Kafka

A thread should be used in a long running process that would block the UI from updating. If it's more than a second or two you might want to put it into a background thread and notify the user with a dialog or spinner or something. If you lock the UI thread for more than 5 seconds the user will be prompted with a "kill or wait" option by the OS.

应该在长时间运行的进程中使用线程来阻止 UI 更新。如果超过一两秒钟,您可能希望将其放入后台线程并通过对话框或微调器或其他方式通知用户。如果您锁定 UI 线程超过 5 秒,操作系统将提示用户“终止或等待”选项。

A service does not run on separate thread, so it will block the UI, but you can spawn a new thread within a service. A service is used more for something that should happen on an interval or keep running/checking for something when there is no UI shown.

服务不在单独的线程上运行,因此它会阻塞 UI,但您可以在服务中生成一个新线程。服务更多地用于应该在某个时间间隔内发生的事情,或者在没有显示 UI 时继续运行/检查某事。

回答by JimmyB

Update:It seems the Android documentation includes a corresponding clarification now, see http://developer.android.com/reference/android/app/Service.html#WhatIsAService.

更新:Android 文档现在似乎包含相应的说明,请参阅http://developer.android.com/reference/android/app/Service.html#WhatIsAService

Original answer:

原答案:

In Android, a Servicedoes not provide any concurrent execution ("run in background"). It is actually more of a simple Java object which merely is instantiated (and managed) via the Android system instead of your application via new.

在 Android 中,aService不提供任何并发执行(“后台运行”)。它实际上更像是一个简单的 Java 对象,它只是通过 Android 系统而不是您的应用程序通过new.

The most important property of a service is therefore not about deferring workload; this can be achieved with simple threads.

因此,服务最重要的特性不是推迟工作量;这可以通过简单的线程来实现。

What makes a service object special is that it is registered with the Android system as a service. This let's the system know that this object provides some sort of service and shouldbe kept alive as long as possible, or until it is stopped. Normal application threads do not have this special meaning to the Android system and will be terminated much more generously at the discretion of the system.

服务对象的特殊之处在于它在 Android 系统中注册为服务。这让系统知道这个对象提供某种服务并且应该尽可能长时间地保持活动状态,或者直到它被停止。普通的应用线程对Android系统没有这种特殊的意义,会根据系统的判断更加慷慨地终止。

So, if you need some background activities to go on only while your application/Activityis active, a thread can do what you need.

因此,如果您需要一些后台活动仅在您的应用程序/Activity处于活动状态时继续,则线程可以执行您需要的操作。

If you need a component that keeps activewill not be purged even when, after a while, the Android system decides to remove your Activitiesfrom memory, you should go for the service, or even a "foreground service", which is deemed even more important by the system and even less likely to be terminated to reclaim resources.

如果您需要一个保持活动状态的组件,即使过了一段时间,Android 系统决定将您Activities从内存中删除,您也应该选择该服务,甚至是“前台服务”,这被认为更为重要更不可能被系统终止回收资源。

Of course, if desired, a Serviceobject can also be made to contain one or more Threadinstances which could then live as long as the Serviceobject itself.

当然,如果需要,Service也可以使一个对象包含一个或多个Thread实例,这些实例可以与Service对象本身一样长。

Edit:

编辑:

Oh, plus: A service is, of course, theway to go if you want to provide some service(s) to other applications, which can "bind" to a service only.

哦,另外:当然,如果您想向其他应用程序提供某些服务,服务是要走路,这些服务只能“绑定”到服务。

回答by Alex Gitelman

Use service if you need something that is either used by other applications or outlives your application activities. The good example of service is file transfer that may take long time and you don't want to force user using your application during this time. Use thread (usually via AsyncTask or similar) in other cases.

如果您需要被其他应用程序使用或比您的应用程序活动寿命更长的东西,请使用服务。服务的一个很好的例子是文件传输,这可能需要很长时间,并且您不希望在此期间强迫用户使用您的应用程序。在其他情况下使用线程(通常通过 AsyncTask 或类似方式)。

For authentication purposes AsyncTaskseems like a good choice.

出于身份验证目的AsyncTask似乎是一个不错的选择。

回答by mc.dev

I believe the main difference is about Android system attitude. Service is a part of android infrastructure, so android recognizes service as a working part of application and considers killing service as a last option. Moreover, you can tune up service priority in order to do it as important as foreground activity. As for threads, android does not recognize a thread as important part which must be kept. So usual threads has much more chances to be killed.

我认为主要区别在于Android系统态度。服务是android基础设施的一部分,因此android将服务识别为应用程序的工作部分,并将杀死服务作为最后的选择。此外,您可以调整服务优先级,使其与前台活动一样重要。至于线程,android 不认为线程是必须保留的重要部分。所以通常的线程有更多的机会被杀死。

For instance If you have an activity which start a working thread and then go background, as android do not recognize thread as a working part, it may think that application do nothing, because no activity or service running and kill the whole app, including the working thread.

例如,如果您有一个活动,它启动一个工作线程然后进入后台,因为 android 不将线程识别为工作部分,它可能认为该应用程序什么都不做,因为没有活动或服务运行并杀死整个应用程序,包括工作线程。

回答by Rikin Prajapati

As per Android Developer Guide (http://developer.android.com/guide/components/services.html#Basics) :

根据 Android 开发人员指南 ( http://developer.android.com/guide/components/services.html#Basics):

A service is simply a component that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need.

服务只是一个可以在后台运行的组件,即使用户没有与您的应用程序交互。因此,只有在您需要时才应该创建服务。

If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class. See the Processes and Threading document for more information about threads.

如果您需要在主线程之外执行工作,但仅在用户与您的应用程序交互时执行,那么您可能应该创建一个新线程而不是服务。例如,如果您想播放一些音乐,但仅在您的活动正在运行时,您可以在 onCreate() 中创建一个线程,在 onStart() 中开始运行它,然后在 onStop() 中停止它。还可以考虑使用 AsyncTask 或 HandlerThread,而不是传统的 Thread 类。有关线程的更多信息,请参阅进程和线程文档。

Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.

请记住,如果您确实使用了服务,默认情况下它仍会在应用程序的主线程中运行,因此如果它执行密集或阻塞操作,您仍应在该服务中创建一个新线程。