同期アダプターを使用してデータを転送する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
注: ほとんどのバックグラウンド処理のユースケースで、推奨ソリューションとして WorkManager をおすすめします。最適なソリューションについては、バックグラウンド処理ガイドをご覧ください。
Android デバイスとウェブサーバー間でデータを同期すると、アプリの有用性と魅力が大幅に高まります。たとえば、ウェブサーバーにデータを転送すると、有用なバックアップになり、サーバーからデータを転送すると、デバイスがオフラインのときでもデータを利用できます。ウェブ インターフェースでデータの入力や編集をした後、そのデータをデバイスで利用する方が簡単な場合もあります。また、長期にわたってデータを収集し、中央のストレージ領域にアップロードしたい場合もあります。
アプリ内でデータ転送を行う独自のシステムを設計することもできますが、Android の同期アダプター フレームワークの使用を検討する必要があります。このフレームワークは、データ転送の管理と自動化に役立ち、さまざまなアプリ間の同期オペレーションを調整します。このフレームワークを使用すると、自身で設計したデータ転送スキームでは利用できない機能を利用できるようになります。
-
プラグイン アーキテクチャ
- 呼び出し可能コンポーネントの形式でデータ転送コードをシステムに追加できます。
-
自動実行
-
データの変更、経過時間、時間帯などのさまざまな条件に基づいてデータ転送を自動化できます。また、実行できない転送はキューに追加され、可能であれば実行されます。
-
自動ネットワーク チェック
- デバイスがネットワークに接続されている場合にのみ、データ転送が実行されます。
-
バッテリー性能の向上
- アプリのデータ転送タスクをすべて 1 か所に集約し、すべてのタスクを同時に実行できます。データ転送も、他のアプリからのデータ転送と連動してスケジュールされます。これらの要因により、システムがネットワークをオンにする回数が減少し、バッテリー使用量が減少します。
-
アカウント管理と認証
-
アプリでユーザー認証情報やサーバー ログインが必要な場合は、必要に応じてアカウント管理と認証をデータ転送に統合できます。
このクラスでは、同期アダプターとそれをラップするバインドされた Service
を作成する方法、同期アダプターをフレームワークに接続するための他のコンポーネントを提供する方法、同期アダプターを実行してさまざまな方法で実行する方法について説明します。
注: 同期アダプターは非同期的に実行されるため、データを即時にではなく定期的かつ効率的に転送することを想定して使用する必要があります。リアルタイムでデータ転送を行う必要がある場合は、AsyncTask
または IntentService
で実行する必要があります。
レッスン
-
スタブ認証システムを作成する
-
同期アダプター フレームワークがアプリの一部であると想定しているアカウント処理コンポーネントを追加する方法を学習します。このレッスンでは、シンプルにするためにスタブ認証コンポーネントを作成する方法について説明します。
-
スタブ コンテンツ プロバイダを作成する
- 同期アダプター フレームワークがアプリの一部であると想定されるコンテンツ プロバイダ コンポーネントを追加する方法を学習します。このレッスンでは、アプリがコンテンツ プロバイダを使用しないことを前提としています。そのため、スタブ コンポーネントを追加する方法について説明します。アプリにコンテンツ プロバイダがすでに存在する場合は、このレッスンをスキップできます。
-
同期アダプターを作成する
- 同期アダプター フレームワークが自動的に実行できるコンポーネントにデータ転送コードをカプセル化する方法を学びます。
-
同期アダプターを実行する
- 同期アダプター フレームワークを使用してデータ転送をトリガーし、スケジュールする方法を学習します。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-27 UTC。
[null,null,["最終更新日 2025-07-27 UTC。"],[],[],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."]]