העברת נתונים באמצעות מתאמי סנכרון
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
הערה: אנחנו ממליצים על WorkManager כפתרון המומלץ לרוב תרחישי השימוש של עיבוד ברקע. כדאי לעיין במדריך לעיבוד ברקע כדי להבין איזו שיטה הכי מתאימה לכם.
סנכרון נתונים בין מכשיר Android לבין שרתי אינטרנט יכול להפוך את האפליקציה שלכם לשימושית ומושכת הרבה יותר עבור המשתמשים. לדוגמה, העברת נתונים לשרת אינטרנט יוצרת גיבוי שימושי, והעברת נתונים משרת מאפשרת למשתמש לגשת לנתונים גם כשהמכשיר במצב אופליין. במקרים מסוימים, למשתמשים יהיה קל יותר להזין ולערוך את הנתונים שלהם בממשק אינטרנט, ואז להפוך את הנתונים האלה לזמינים במכשיר שלהם. הם גם יכולים לאסוף נתונים לאורך זמן ואז להעלות אותם לאזור אחסון מרכזי.
אפשר לעצב מערכת משלכם להעברת נתונים באפליקציה, אבל מומלץ להשתמש במסגרת של מתאם הסנכרון של Android. המסגרת הזו עוזרת לנהל ולהפוך לאוטומטי את העברות הנתונים, ומתאמת פעולות סנכרון בין אפליקציות שונות. כשמשתמשים במסגרת הזו, אפשר ליהנות מכמה תכונות שלא זמינות לתוכניות להעברת נתונים שאתם יוצרים בעצמכם:
-
ארכיטקטורה של תוספים
-
מאפשר להוסיף למערכת קוד להעברת נתונים בצורה של רכיבים שאפשר להפעיל.
-
הרצה אוטומטית
-
מאפשרת לבצע אוטומציה של העברת נתונים על סמך מגוון קריטריונים, כולל שינויים בנתונים,
זמן שחלף או שעה ביום. בנוסף, המערכת מוסיפה לתור העברות שלא ניתן להפעיל, ומפעילה אותן כשאפשר.
-
בדיקה אוטומטית של הרשת
-
המערכת מפעילה את העברת הנתונים רק כשיש למכשיר קישוריות לרשת.
-
ביצועי סוללה משופרים
-
מאפשרת לרכז את כל משימות העברת הנתונים של האפליקציה במקום אחד, כך שכולן יפעלו בו-זמנית. העברת הנתונים שלכם מתוזמנת גם יחד עם העברות נתונים מאפליקציות אחרות. הגורמים האלה מצמצמים את מספר הפעמים שבהן המערכת צריכה להפעיל את הרשת, וכך מצטמצם השימוש בסוללה.
-
ניהול חשבון ואימות
-
אם האפליקציה שלכם דורשת פרטי כניסה של משתמש או כניסה לשרת, אתם יכולים לשלב את ניהול החשבון והאימות בהעברת הנתונים.
בשיעור הזה נסביר איך ליצור מתאם סנכרון ואת Service
שקשור אליו, איך לספק את הרכיבים האחרים שעוזרים לחבר את מתאם הסנכרון למסגרת ואיך להפעיל את מתאם הסנכרון בדרכים שונות.
הערה: מתאמי סנכרון פועלים באופן אסינכרוני, ולכן צריך להשתמש בהם בהנחה שהם מעבירים נתונים באופן קבוע ויעיל, אבל לא באופן מיידי. אם אתם צריכים להעביר נתונים בזמן אמת, עליכם לעשות זאת ב-AsyncTask
או ב-IntentService
.
שיעורי מוזיקה
-
יצירת מאמת חשבונות פיקטיבי
-
במאמר הזה נסביר איך להוסיף רכיב לטיפול בחשבון, שצפוי להיות חלק מהאפליקציה במסגרת מתאם הסנכרון. כדי לפשט את התהליך, נראה לכם איך ליצור רכיב אימות מסוג stub.
-
יצירת ספק תוכן פיקטיבי
-
בשיעור הזה נסביר איך להוסיף רכיב של ספק תוכן שאמור להיות חלק מהאפליקציה לפי מסגרת מתאם הסנכרון. אנחנו מניחים שהאפליקציה לא משתמשת בספק תוכן, ולכן נראה לכם איך להוסיף רכיב stub. אם כבר יש לכם ספק תוכן באפליקציה, אתם יכולים לדלג על השיעור הזה.
-
יצירת מתאם סנכרון
-
כאן מוסבר איך להוסיף את קוד העברת הנתונים לרכיב שאפשר להפעיל באופן אוטומטי באמצעות
מסגרת מתאמי הסנכרון.
-
הפעלת מתאם סנכרון
-
כאן מוסבר איך להפעיל העברות נתונים ולתזמן אותן באמצעות מסגרת מתאם הסנכרון.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. 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."]]