一个无头片段,并在Android的服务之间的区别是什么? [英] What is the difference between a Headless Fragment and a Service in Android?

查看:117
本文介绍了一个无头片段,并在Android的服务之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个服务是一种应用程序组件重新presenting无论是应用程序的愿望,执行长时间运行的操作,而不是与用户交互或提供功能的其他应用程序使用。

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

片段可以使用而不定义用户界面。它建议使用无头片段为您的后台处理。

Fragments can be used without defining a user interface. It is recommended to use headless fragments for your background processing.

这里的区别是什么?

推荐答案

服务 s的的优先级高于活动 S上的过程级。当内存不足时,Android系统将优先服务结束了活动 s,因而服务■对于长时间运行的任务的理想选择。请参阅文章,题为进程和线程了解详情。

Services are given higher priority than Activitys at the process-level. When memory is low, the Android system will prioritize Services over Activitys, making Services the ideal option for long-running tasks. See the article titled Processes and Threads for more information.

此外,当你的状态下在你原来的职位:

Also, when you state the following in your original post:

片段可以使用而不定义用户界面。它建议使用无头片段为您的后台处理。

Fragments can be used without defining a user interface. It is recommended to use headless fragments for your background processing.

你从哪儿引用此?我同意第一句,但第二句是太一般了。对于短运行的任务(例如执行HTTP请求等),无头片段很好地工作。但是,对于执行长期运行的后台处理(例如下载一个很大的文件等),无头的片段可能不是你想要的。例如,如果你使用一个无头的片段来执行长时间运行的任务,用户点击返回按钮,这将导致两个活动和它的无头片段被消除了。

Where are you quoting this from? I agree with the first sentence, but the second sentence is too general. For short-running tasks (such as performing HTTP requests, etc.), headless fragments work fine. However, for performing long-running background processing (such as downloading a very large file, etc.) a headless fragment may not be what you want. For example, if you used a headless fragment to perform a long-running task and the user clicked the "back button", this will cause both the Activity and its headless Fragment to be destroyed.

总之,服务是存在独立的活动的背景部件,这意味着它可以继续在后台运行,即使活动赖以起家的服务被销毁。而另一方面,一个无头的片段总是有关联的父活动。如果活动承载片段是由系统破坏,那么该片段将被杀死也。

To summarize, a service is a background component that exists independent of an Activity, meaning that it can continue to run in the background even if the Activity which started the service is destroyed. On the other hand, a headless fragment will always have an associated parent Activity. If the Activity that hosts the fragment is destroyed by the system, then the fragment will have to be killed as well.

这篇关于一个无头片段,并在Android的服务之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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