螺纹和服务机器人之间的区别是什么,下载图像时螺纹、机器人、和服务、图像

2023-09-04 07:31:29 作者:独遇不与

在Android的线程和服务之间的区别是什么,下载图像时

What is the difference between thread and service in android,when downloading an image

推荐答案

除了极少数例外,你永远不应该明确地创建一个。线程是昂贵的,容易编程错误。使用的AsyncTask ,因为它处理的线程安全的复杂性,并提供了线程池的优化。或者更好的是,如果网络活动的理由做工作的主线之外,使用,管理所有这些问题对您的许多网络图书馆之一。哪种方法是最快的不是东西,一般都可以回答,并不应该甚至是关心,直到你已经尝试了简单而明确的解决方案,并证明了它的性能是不够的。

With rare exceptions, you should never explicitly create a Thread. Threads are expensive and prone to programmer error. Use AsyncTask because it handles the complexity of thread safety and provides the optimization of thread pooling. Or better yet, if network activity is your reason for doing work outside the main thread, use one of the many network libraries that manages all of these concerns for you. Which approach is fastest is not something that can be answered generally, and should never even be a concern until you've tried the simple and clear solution and demonstrated that its performance is inadequate.

无论你如何让你的网络活动同步,这是不是一个单一的活动的生命周期内开始并完成(或取消)的任何网络活动实例需要主持别的东西。如果只需要跨越的配置变化中生存,它托管在保留片段。如果需要不同的活动之间的生存,它托管在服务。在这些选项之间进行选择,请记住,你的活动可能会破坏它进入背景或backstack任何时候。

Regardless of how you make your network activity asynchronous, any network activity that is not started and completed (or cancelled) within the lifetime of a single Activity instance needs to be hosted in something else. If it only needs to survive across configuration changes, host it in a retained Fragment. If it needs to survive between different activities, host it in a Service. When choosing between these options, remember that your Activity may be destroyed any time it goes into the background or backstack.