使用同步轉換介面轉移資料
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
注意:對於大多數背景處理用途,我們建議使用 WorkManager 做為建議解決方案。請參閱背景處理指南,瞭解最適合您的解決方案。
在 Android 裝置和網路伺服器之間同步處理資料,可以大幅提高應用程式的實用性,對使用者來說更具吸引力。舉例來說,將資料轉移至網路伺服器會產生實用的備份,而從伺服器轉移資料可讓使用者即使裝置處於離線狀態,也能存取這些資料。在某些情況下,使用者可能會發現更容易在網頁介面中輸入及編輯資料,然後將這些資料儲存在裝置中;或者,使用者也可以長期收集資料,然後上傳到中央儲存空間區域。
雖然您可以設計用來在應用程式中轉移資料的專屬系統,但我們仍建議您考慮使用 Android 的同步轉換介面架構。這個架構可協助您管理及自動化資料移轉,以及協調不同應用程式的同步處理作業。使用這個架構時,您可以善用自行設計的資料移轉配置不支援的功能:
-
外掛程式架構
- 可讓您以可呼叫元件的形式,在系統中新增資料移轉程式碼。
-
自動執行
-
可讓您根據各種條件 (包括資料變更、經過時間或時段) 自動移轉資料。此外,系統會將無法執行的移轉作業新增至佇列,並盡可能執行這些移轉作業。
-
自動網路檢查
- 只有在裝置具備網路連線時,系統才會執行資料傳輸作業。
-
改善電池效能
- 可讓您將應用程式的所有資料移轉工作集中在一處,以便同時執行。系統也會安排資料轉移作業,同時配合其他應用程式的資料轉移工作。這些因素可減少系統必須連上網路的次數,進而降低電池用量。
-
帳戶管理與驗證
-
如果應用程式需要使用者憑證或伺服器登入,您可以選擇將帳戶管理和驗證功能整合至資料移轉。
本課程將說明如何建立同步轉換介面和包裝的 Service
、提供其他元件,協助您將同步轉接器插入架構,以及如何執行同步轉換介面,以各種方式執行。
注意:同步轉換介面會以非同步的方式執行,因此您應搭配上述轉接程式使用,以便定期有效傳輸資料,但無法立即傳輸資料。如需進行即時資料傳輸,請在 AsyncTask
或 IntentService
中執行。
課程
-
建立虛設常式驗證器
-
瞭解如何新增同步轉接程式架構預期屬於應用程式的一部分帳戶處理元件。本課程說明如何建立簡單易懂的虛設常式驗證元件。
-
建立虛設常式內容供應器
- 瞭解如何新增同步轉換介面架構預期屬於應用程式的內容供應器元件。本課程假設應用程式未使用內容供應器,因此會說明如何新增虛設常式元件。如果應用程式中已有內容供應器,您可以略過這個課程。
-
建立同步轉換介面
-
瞭解如何在同步轉換介面架構可自動執行的元件中封裝資料移轉程式碼。
-
執行同步轉換介面
-
瞭解如何使用同步轉換介面架構觸發及安排資料移轉作業。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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."]]