โอนข้อมูลโดยใช้อะแดปเตอร์การซิงค์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หมายเหตุ: เราขอแนะนำให้ใช้ WorkManager
เป็นโซลูชันที่แนะนำสำหรับกรณีการใช้งานการประมวลผลในเบื้องหลังส่วนใหญ่ โปรดดูคู่มือการประมวลผลในเบื้องหลังเพื่อดูว่าโซลูชันใดเหมาะกับคุณที่สุด
การซิงค์ข้อมูลระหว่างอุปกรณ์ Android กับเว็บเซิร์ฟเวอร์จะช่วยให้แอปพลิเคชันของคุณมีประโยชน์และน่าสนใจสำหรับผู้ใช้มากขึ้นอย่างมาก
เช่น การโอนข้อมูลไปยังเว็บเซิร์ฟเวอร์
จะช่วยให้มีข้อมูลสำรองที่มีประโยชน์ และการโอนข้อมูลจากเซิร์ฟเวอร์จะทำให้ผู้ใช้เข้าถึงข้อมูลได้
แม้ว่าอุปกรณ์จะออฟไลน์อยู่ก็ตาม ในบางกรณี ผู้ใช้อาจพบว่าการป้อนและแก้ไขข้อมูลในอินเทอร์เฟซเว็บนั้นง่ายกว่า แล้วจึงนำข้อมูลดังกล่าวไปใช้ในอุปกรณ์ หรืออาจต้องการรวบรวมข้อมูลเมื่อเวลาผ่านไป แล้วจึงอัปโหลดไปยังพื้นที่เก็บข้อมูลส่วนกลาง
แม้ว่าคุณจะออกแบบระบบของคุณเองเพื่อทำการโอนข้อมูลในแอปได้ แต่คุณควรพิจารณาใช้เฟรมเวิร์ก Sync Adapter ของ Android
เฟรมเวิร์กนี้ช่วยจัดการและทำให้การโอนข้อมูลเป็นแบบอัตโนมัติ
รวมถึงประสานงานการซิงค์ในแอปต่างๆ เมื่อใช้
เฟรมเวิร์กนี้ คุณจะใช้ประโยชน์จากฟีเจอร์ต่างๆ ที่ไม่มีในรูปแบบการโอนข้อมูล
ที่คุณออกแบบเองได้
-
สถาปัตยกรรมปลั๊กอิน
-
ช่วยให้คุณเพิ่มโค้ดการโอนข้อมูลลงในระบบในรูปแบบของคอมโพเนนต์ที่เรียกใช้ได้
-
การดำเนินการอัตโนมัติ
-
ช่วยให้คุณโอนข้อมูลโดยอัตโนมัติตามเกณฑ์ต่างๆ รวมถึงการเปลี่ยนแปลงข้อมูล
เวลาที่ผ่านไป หรือเวลาของวัน นอกจากนี้ ระบบจะเพิ่มการโอนที่เรียกใช้ไม่ได้
ลงในคิว และเรียกใช้เมื่อเป็นไปได้
-
การตรวจสอบเครือข่ายอัตโนมัติ
-
ระบบจะเรียกใช้การโอนข้อมูลเมื่ออุปกรณ์มีการเชื่อมต่อเครือข่ายเท่านั้น
-
ประสิทธิภาพแบตเตอรี่ที่ดียิ่งขึ้น
-
ช่วยให้คุณรวมงานการโอนข้อมูลทั้งหมดของแอปไว้ในที่เดียวเพื่อให้งานทั้งหมด
ทำงานพร้อมกันได้ นอกจากนี้ ระบบยังกำหนดเวลาการโอนข้อมูลของคุณร่วมกับการโอนข้อมูลจากแอปอื่นๆ ด้วย
ปัจจัยเหล่านี้จะช่วยลดจำนวนครั้งที่ระบบต้อง
เปิดเครือข่าย ซึ่งจะช่วยลดการใช้แบตเตอรี่
-
การจัดการบัญชีและการตรวจสอบสิทธิ์
-
หากแอปของคุณต้องใช้ข้อมูลเข้าสู่ระบบของผู้ใช้หรือการเข้าสู่ระบบเซิร์ฟเวอร์ คุณจะเลือกผสานรวมการจัดการบัญชีและการตรวจสอบสิทธิ์เข้ากับการโอนข้อมูลได้
คลาสนี้จะแสดงวิธีสร้าง Sync Adapter และ Service
ที่
ห่อหุ้ม Sync Adapter วิธีจัดหาคอมโพเนนต์อื่นๆ ที่ช่วยให้คุณเสียบ Sync Adapter เข้ากับ
เฟรมเวิร์ก และวิธีเรียกใช้ Sync Adapter ในรูปแบบต่างๆ
หมายเหตุ: อะแดปเตอร์การซิงค์จะทำงานแบบไม่พร้อมกัน ดังนั้นคุณควรใช้อะแดปเตอร์ดังกล่าวโดย
คาดหวังว่าอะแดปเตอร์จะโอนข้อมูลอย่างสม่ำเสมอและมีประสิทธิภาพ แต่จะไม่โอนข้อมูลทันที หาก
คุณต้องการโอนข้อมูลแบบเรียลไทม์ คุณควรทำใน AsyncTask
หรือ
IntentService
บทเรียน
-
สร้าง Stub Authenticator
-
ดูวิธีเพิ่มคอมโพเนนต์การจัดการบัญชีที่เฟรมเวิร์กของ Sync Adapter คาดหวังให้เป็น
ส่วนหนึ่งของแอป บทเรียนนี้จะแสดงวิธีสร้างคอมโพเนนต์การตรวจสอบสิทธิ์แบบ Stub เพื่อให้
ง่ายต่อการใช้งาน
-
สร้างผู้ให้บริการเนื้อหา Stub
-
ดูวิธีเพิ่มคอมโพเนนต์ผู้ให้บริการเนื้อหาที่เฟรมเวิร์กของ Sync Adapter คาดหวังให้เป็น
ส่วนหนึ่งของแอป บทเรียนนี้ถือว่าแอปของคุณไม่ได้ใช้ Content Provider จึงแสดงวิธีเพิ่มคอมโพเนนต์ Stub หากมีผู้ให้บริการเนื้อหาในแอปอยู่แล้ว
คุณสามารถข้ามบทเรียนนี้ได้
-
สร้างอะแดปเตอร์การซิงค์
-
ดูวิธีห่อหุ้มโค้ดการโอนข้อมูลในคอมโพเนนต์ที่เฟรมเวิร์กอะแดปเตอร์การซิงค์
เรียกใช้ได้โดยอัตโนมัติ
-
เรียกใช้อะแดปเตอร์การซิงค์
-
ดูวิธีทริกเกอร์และกำหนดเวลาการโอนข้อมูลโดยใช้เฟรมเวิร์กอะแดปเตอร์การซิงค์
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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 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."]]