使用同步适配器传输数据
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
注意:对于大多数后台处理用例,我们建议使用 WorkManager 作为解决方案。请参阅后台处理指南,了解哪种解决方案最适合您。
在 Android 设备和 Web 服务器之间同步数据,可以显著提高应用对用户的实用性和吸引力。例如,将数据传输到网络服务器可以实现有用的备份,从服务器传输数据可让用户在设备离线时也能使用这些数据。在某些情况下,用户可能会发现在网页界面中输入和修改数据,然后在设备上使用这些数据更方便;或者,他们可能希望随着时间的推移收集数据,然后将其上传到中央存储区域。
尽管您可以设计自己的系统用于在应用中执行数据传输,但也应考虑使用 Android 的同步适配器框架。此框架有助于管理和自动执行数据传输,以及协调不同应用之间的同步操作。使用此框架时,您可以利用一些您自己设计的数据传输方案所不具备的功能:
-
插件架构
-
允许您以 Callable 组件的形式向系统添加数据传输代码。
-
自动执行
-
允许您根据各种条件(包括数据更改、经过的时间或一天中的时间)自动执行数据传输。此外,系统会添加无法运行的传输到队列,并在可能的情况下运行它们。
-
自动检查网络
-
系统仅在设备连接到网络时运行数据传输。
-
提升了电池性能
-
可让您将应用的所有数据传输任务集中到一个地方,以便它们同时运行。您的数据传输还会与其他应用的数据传输一起安排。这些因素可减少系统需要开启网络的次数,从而减少电池用量。
-
帐号管理和身份验证
-
如果您的应用要求提供用户凭据或服务器登录信息,您可以选择将帐号管理和身份验证集成到数据传输中。
本课程将向您介绍如何创建同步适配器及其封装的绑定 Service
、如何提供可帮助您将同步适配器插入框架的其他组件,以及如何以各种方式运行同步适配器。
注意:同步适配器是异步运行的,因此您应该期望它们能够定期、高效地传输数据,但不是即时地传输数据。如果您需要进行实时数据传输,则应在 AsyncTask
或 IntentService
中执行此操作。
课程
-
创建桩身份验证器
-
了解如何添加同步适配器框架要求您的应用具备的帐号处理组件。为简单起见,本课介绍了如何创建桩身份验证组件。
-
创建桩 content provider
-
了解如何添加同步适配器框架要求您的应用具备的 content provider 组件。本课假设您的应用不使用 content provider,因此介绍了如何添加桩组件。如果您的应用中已经有 content provider,则可以跳过本课程。
-
创建同步适配器
-
了解如何将数据传输代码封装在同步适配器框架能够自动运行的组件中。
-
运行同步适配器
-
了解如何使用同步适配器框架触发和调度数据传输。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Transfer data using sync adapters\n\n**Note:** We recommended [WorkManager](/topic/libraries/architecture/workmanager)\nas the recommended solution for most background processing use cases. Please reference the\n[background processing guide](/guide/background) to learn which solution works best for you.\n\n\nSynchronizing data between an Android device and web servers can make your application\nsignificantly more useful and compelling for your users. For example, transferring data to a web\nserver makes a useful backup, and transferring data from a server makes it available to the user\neven when the device is offline. In some cases, users may find it easier to enter and edit their\ndata in a web interface and then have that data available on their device, or they may want to\ncollect data over time and then upload it to a central storage area.\n\n\nAlthough you can design your own system for doing data transfers in your app, you should\nconsider using Android's sync adapter framework. This framework helps manage and automate data\ntransfers, and coordinates synchronization operations across different apps. When you use\nthis framework, you can take advantage of several features that aren't available to data\ntransfer schemes you design yourself:\n\n\nPlug-in architecture\n:\n Allows you to add data transfer code to the system in the form of callable components.\n\n\nAutomated execution\n:\n Allows you to automate data transfer based on a variety of criteria, including data changes,\n elapsed time, or time of day. In addition, the system adds transfers that are unable to\n run to a queue, and runs them when possible.\n\n\nAutomated network checking\n:\n The system only runs your data transfer when the device has network connectivity.\n\n\nImproved battery performance\n:\n Allows you to centralize all of your app's data transfer tasks in one place, so that they\n all run at the same time. Your data transfer is also scheduled in conjunction with data\n transfers from other apps. These factors reduce the number of times the system has to\n switch on the network, which reduces battery usage.\n\n\nAccount management and authentication\n:\n If your app requires user credentials or server login, you can optionally\n integrate account management and authentication into your data transfer.\n\n\nThis class shows you how to create a sync adapter and the bound [Service](/reference/android/app/Service) that\nwraps it, how to provide the other components that help you plug the sync adapter into the\nframework, and how to run the sync adapter to run in various ways.\n\n\n**Note:** Sync adapters run asynchronously, so you should use them with the\nexpectation that they transfer data regularly and efficiently, but not instantaneously. If\nyou need to do real-time data transfer, you should do it in an [AsyncTask](/reference/android/os/AsyncTask) or\nan [IntentService](/reference/android/app/IntentService).\n\nLessons\n-------\n\n\n**[Create a stub authenticator](/training/sync-adapters/creating-authenticator)**\n:\n Learn how to add an account-handling component that the sync adapter framework expects to be\n part of your app. This lesson shows you how to create a stub authentication component for\n simplicity.\n\n\n**[Create a stub content provider](/training/sync-adapters/creating-stub-provider)**\n:\n Learn how to add a content provider component that the sync adapter framework expects to be\n part of your app. This lesson assumes that your app doesn't use a content provider, so it\n shows you how to add a stub component. If you have a content provider already in your app,\n you can skip this lesson.\n\n\n**[Create a sync adapter](/training/sync-adapters/creating-sync-adapter)**\n:\n Learn how to encapsulate your data transfer code in a component that the sync\n adapter framework can run automatically.\n\n\n**[Run a sync adapter](/training/sync-adapters/running-sync-adapter)**\n:\n Learn how to trigger and schedule data transfers using the sync adapter framework."]]