สร้างผู้ให้บริการเนื้อหา Stub
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เฟรมเวิร์กอะแดปเตอร์ซิงค์ได้รับการออกแบบมาให้ทำงานกับข้อมูลอุปกรณ์ที่จัดการโดย
เฟรมเวิร์กผู้ให้บริการเนื้อหาที่มีความปลอดภัยสูง ด้วยเหตุนี้ เฟรมเวิร์กอะแดปเตอร์การซิงค์จึงคาดหวังว่า
แอปที่ใช้เฟรมเวิร์กนี้ได้กำหนดผู้ให้บริการเนื้อหาสำหรับข้อมูลในเครื่องแล้ว
หากเฟรมเวิร์กอะแดปเตอร์การซิงค์พยายามเรียกใช้อะแดปเตอร์การซิงค์ และแอปของคุณไม่มี
Content Provider อะแดปเตอร์การซิงค์เกิดขัดข้อง
หากคุณกำลังพัฒนาแอปใหม่ที่จะโอนข้อมูลจากเซิร์ฟเวอร์ไปยังอุปกรณ์ คุณควร
ควรพิจารณาจัดเก็บข้อมูลในเครื่องไว้ในผู้ให้บริการเนื้อหา นอกเหนือจากความสำคัญต่อ
อะแดปเตอร์การซิงค์ ผู้ให้บริการเนื้อหามีคุณประโยชน์ด้านความปลอดภัยมากมาย และ
ที่ออกแบบมาเพื่อจัดการพื้นที่เก็บข้อมูลในระบบ Android ดูข้อมูลเพิ่มเติมเกี่ยวกับการสร้างเนื้อหา
โปรดดูการสร้างผู้ให้บริการเนื้อหา
แต่หากคุณจัดเก็บข้อมูลในเครื่องในรูปแบบอื่นอยู่แล้ว คุณจะยังใช้การซิงค์ได้
เพื่อจัดการการโอนข้อมูล เพื่อให้เป็นไปตามข้อกำหนดเฟรมเวิร์กอะแดปเตอร์การซิงค์สำหรับ
Content Provider ให้เพิ่มผู้ให้บริการเนื้อหา Stub ลงในแอป ผู้ให้บริการสตับจะนำองค์ประกอบ
คลาสผู้ให้บริการเนื้อหา แต่วิธีการที่จำเป็นทั้งหมดจะแสดงผลเป็น null
หรือ 0
หากคุณ
เพิ่มผู้ให้บริการสตับ คุณก็ใช้อะแดปเตอร์การซิงค์เพื่อโอนข้อมูลจากพื้นที่เก็บข้อมูลใดก็ได้
ที่คุณเลือก
หากมีผู้ให้บริการเนื้อหาในแอปอยู่แล้ว ก็ไม่จำเป็นต้องมีผู้ให้บริการเนื้อหา Stub
ในกรณีนี้ คุณสามารถข้ามบทเรียนนี้และไปยังบทเรียนนั้นต่อ
การสร้างอะแดปเตอร์การซิงค์ หากคุณยังไม่มี
Content Provider บทเรียนนี้จะแสดงวิธีเพิ่มผู้ให้บริการเนื้อหา Stub ที่ช่วยให้คุณทำสิ่งต่อไปนี้ได้
เสียบอะแดปเตอร์การซิงค์เข้ากับเฟรมเวิร์ก
เพิ่มผู้ให้บริการเนื้อหา Stub
หากต้องการสร้างผู้ให้บริการเนื้อหา Stub สำหรับแอป โปรดขยายชั้นเรียน
ContentProvider
และนำวิธีการที่จำเป็นออก ดังต่อไปนี้
ตัวอย่างข้อมูลแสดงวิธีสร้างผู้ให้บริการสตับ
Kotlin
/*
* Define an implementation of ContentProvider that stubs out
* all methods
*/
class StubProvider : ContentProvider() {
/*
* Always return true, indicating that the
* provider loaded correctly.
*/
override fun onCreate(): Boolean = true
/*
* Return no type for MIME type
*/
override fun getType(uri: Uri): String? = null
/*
* query() always returns no results
*
*/
override fun query(
uri: Uri,
projection: Array<String>,
selection: String,
selectionArgs: Array<String>,
sortOrder: String
): Cursor? = null
/*
* insert() always returns null (no URI)
*/
override fun insert(uri: Uri, values: ContentValues): Uri? = null
/*
* delete() always returns "no rows affected" (0)
*/
override fun delete(uri: Uri, selection: String, selectionArgs: Array<String>): Int = 0
/*
* update() always returns "no rows affected" (0)
*/
override fun update(
uri: Uri,
values: ContentValues,
selection: String,
selectionArgs: Array<String>
): Int = 0
}
Java
/*
* Define an implementation of ContentProvider that stubs out
* all methods
*/
public class StubProvider extends ContentProvider {
/*
* Always return true, indicating that the
* provider loaded correctly.
*/
@Override
public boolean onCreate() {
return true;
}
/*
* Return no type for MIME type
*/
@Override
public String getType(Uri uri) {
return null;
}
/*
* query() always returns no results
*
*/
@Override
public Cursor query(
Uri uri,
String[] projection,
String selection,
String[] selectionArgs,
String sortOrder) {
return null;
}
/*
* insert() always returns null (no URI)
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
/*
* delete() always returns "no rows affected" (0)
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
/*
* update() always returns "no rows affected" (0)
*/
public int update(
Uri uri,
ContentValues values,
String selection,
String[] selectionArgs) {
return 0;
}
}
ประกาศผู้ให้บริการในไฟล์ Manifest
เฟรมเวิร์กอะแดปเตอร์การซิงค์จะยืนยันว่าแอปของคุณมีผู้ให้บริการเนื้อหาหรือไม่ โดยตรวจสอบว่า
แอปได้ประกาศผู้ให้บริการในไฟล์ Manifest ของแอป หากต้องการประกาศผู้ให้บริการสตับใน
ไฟล์ Manifest ให้เพิ่มเอลิเมนต์ <provider>
ที่มีแอตทริบิวต์ต่อไปนี้
-
android:name="com.example.android.datasync.provider.StubProvider"
-
ระบุชื่อที่มีคุณสมบัติครบถ้วนของคลาสที่ใช้ผู้ให้บริการเนื้อหา Stub
-
android:authorities="com.example.android.datasync.provider"
-
สิทธิ์ URI ที่ระบุผู้ให้บริการเนื้อหา Stub ทำให้ค่านี้
ชื่อแพ็กเกจที่มีสตริง ".provider" ต่อท้าย แม้ว่าคุณจะประกาศ
ระบบไม่ได้ดำเนินการใดๆ กับคำขอเข้าถึงผู้ให้บริการเอง
-
android:exported="false"
-
กำหนดว่าแอปอื่นๆ สามารถเข้าถึงผู้ให้บริการเนื้อหาได้หรือไม่ สำหรับเนื้อหาต้นขั้ว
ให้กำหนดค่าเป็น
false
เนื่องจากไม่จำเป็นต้องอนุญาตให้แอปอื่นๆ ดู
กับผู้ให้บริการ ค่านี้ไม่ส่งผลต่อการโต้ตอบระหว่างเฟรมเวิร์กอะแดปเตอร์การซิงค์
และผู้ให้บริการเนื้อหา
-
android:syncable="true"
-
ตั้งค่าแฟล็กที่ระบุว่าผู้ให้บริการซิงค์ได้ หากตั้งค่าสถานะนี้เป็น
true
คุณไม่จำเป็นต้องโทรหา setIsSyncable()
ด้วยรหัสของคุณ ธงนี้อนุญาตให้เฟรมเวิร์กอะแดปเตอร์การซิงค์สร้างข้อมูล
การโอนกับผู้ให้บริการเนื้อหา แต่การโอนจะเกิดขึ้นเฉพาะในกรณีที่ดำเนินการอย่างชัดแจ้งเท่านั้น
ข้อมูลโค้ดต่อไปนี้จะแสดงวิธีเพิ่ม
องค์ประกอบ <provider>
ไปยังไฟล์ Manifest ของแอป:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.network.sync.BasicSyncAdapter"
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
...
<provider
android:name="com.example.android.datasync.provider.StubProvider"
android:authorities="com.example.android.datasync.provider"
android:exported="false"
android:syncable="true"/>
...
</application>
</manifest>
เมื่อสร้างทรัพยากร Dependency ที่เฟรมเวิร์กอะแดปเตอร์การซิงค์ต้องการแล้ว คุณจะทำสิ่งต่อไปนี้ได้
สร้างคอมโพเนนต์สรุปโค้ดการโอนข้อมูล คอมโพเนนต์นี้เรียกว่า
อะแดปเตอร์การซิงค์ บทเรียนถัดไปจะแสดงวิธีเพิ่มคอมโพเนนต์นี้ลงในแอปของคุณ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา 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,["# Create a stub content provider\n\nThe sync adapter framework is designed to work with device data managed by the flexible and\nhighly secure content provider framework. For this reason, the sync adapter framework expects\nthat an app that uses the framework has already defined a content provider for its local data.\nIf the sync adapter framework tries to run your sync adapter, and your app doesn't have a\ncontent provider, your sync adapter crashes.\n\n\nIf you're developing a new app that transfers data from a server to the device, you should\nstrongly consider storing the local data in a content provider. Besides their importance for\nsync adapters, content providers offer a variety of security benefits and are specifically\ndesigned to handle data storage on Android systems. To learn more about creating a content\nprovider, see [Creating a Content Provider](/guide/topics/providers/content-provider-creating).\n\n\nHowever, if you're already storing local data in another form, you can still use a sync\nadapter to handle data transfer. To satisfy the sync adapter framework requirement for a\ncontent provider, add a stub content provider to your app. A stub provider implements the\ncontent provider class, but all of its required methods return `null` or `0`. If you\nadd a stub provider, you can then use a sync adapter to transfer data from any storage\nmechanism you choose.\n\n\nIf you already have a content provider in your app, you don't need a stub content provider.\nIn that case, you can skip this lesson and proceed to the lesson\n[Creating a Sync Adapter](/training/sync-adapters/creating-sync-adapter). If you don't yet have a\ncontent provider, this lesson shows you how to add a stub content provider that allows you to\nplug your sync adapter into the framework.\n\nAdd a stub content provider\n---------------------------\n\n\nTo create a stub content provider for your app, extend the class\n[ContentProvider](/reference/android/content/ContentProvider) and stub out its required methods. The following\nsnippet shows you how to create the stub provider: \n\n### Kotlin\n\n```kotlin\n/*\n * Define an implementation of ContentProvider that stubs out\n * all methods\n */\nclass StubProvider : ContentProvider() {\n /*\n * Always return true, indicating that the\n * provider loaded correctly.\n */\n override fun onCreate(): Boolean = true\n\n /*\n * Return no type for MIME type\n */\n override fun getType(uri: Uri): String? = null\n\n /*\n * query() always returns no results\n *\n */\n override fun query(\n uri: Uri,\n projection: Array\u003cString\u003e,\n selection: String,\n selectionArgs: Array\u003cString\u003e,\n sortOrder: String\n ): Cursor? = null\n\n /*\n * insert() always returns null (no URI)\n */\n override fun insert(uri: Uri, values: ContentValues): Uri? = null\n\n /*\n * delete() always returns \"no rows affected\" (0)\n */\n override fun delete(uri: Uri, selection: String, selectionArgs: Array\u003cString\u003e): Int = 0\n\n /*\n * update() always returns \"no rows affected\" (0)\n */\n override fun update(\n uri: Uri,\n values: ContentValues,\n selection: String,\n selectionArgs: Array\u003cString\u003e\n ): Int = 0\n}\n```\n\n### Java\n\n```java\n/*\n * Define an implementation of ContentProvider that stubs out\n * all methods\n */\npublic class StubProvider extends ContentProvider {\n /*\n * Always return true, indicating that the\n * provider loaded correctly.\n */\n @Override\n public boolean onCreate() {\n return true;\n }\n /*\n * Return no type for MIME type\n */\n @Override\n public String getType(Uri uri) {\n return null;\n }\n /*\n * query() always returns no results\n *\n */\n @Override\n public Cursor query(\n Uri uri,\n String[] projection,\n String selection,\n String[] selectionArgs,\n String sortOrder) {\n return null;\n }\n /*\n * insert() always returns null (no URI)\n */\n @Override\n public Uri insert(Uri uri, ContentValues values) {\n return null;\n }\n /*\n * delete() always returns \"no rows affected\" (0)\n */\n @Override\n public int delete(Uri uri, String selection, String[] selectionArgs) {\n return 0;\n }\n /*\n * update() always returns \"no rows affected\" (0)\n */\n public int update(\n Uri uri,\n ContentValues values,\n String selection,\n String[] selectionArgs) {\n return 0;\n }\n}\n```\n\nDeclare the provider in the manifest\n------------------------------------\n\n\nThe sync adapter framework verifies that your app has a content provider by checking that your\napp has declared a provider in its app manifest. To declare the stub provider in the\nmanifest, add a [\u003cprovider\u003e](/guide/topics/manifest/provider-element) element with the following attributes:\n\n\n`android:name=\"com.example.android.datasync.provider.StubProvider\"`\n:\n Specifies the fully-qualified name of the class that implements the stub content provider.\n\n\n`android:authorities=\"com.example.android.datasync.provider\"`\n:\n A URI authority that identifies the stub content provider. Make this value your app's\n package name with the string \".provider\" appended to it. Even though you're declaring your\n stub provider to the system, nothing tries to access the provider itself.\n\n\n`android:exported=\"false\"`\n:\n Determines whether other apps can access the content provider. For your stub content\n provider, set the value to `false`, since there's no need to allow other apps to see\n the provider. This value doesn't affect the interaction between the sync adapter framework\n and the content provider.\n\n\n`android:syncable=\"true\"`\n:\n Sets a flag that indicates that the provider is syncable. If you set this flag to\n `true`, you don't have to call [setIsSyncable()](/reference/android/content/ContentResolver#setIsSyncable(android.accounts.Account, java.lang.String, int)) in your code. The flag allows the sync adapter framework to make data\n transfers with the content provider, but transfers only occur if you do them explicitly.\n\n\nThe following snippet shows you how to add the\n[\u003cprovider\u003e](/guide/topics/manifest/provider-element) element to the app manifest: \n\n```xml\n\u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n package=\"com.example.android.network.sync.BasicSyncAdapter\"\n android:versionCode=\"1\"\n android:versionName=\"1.0\" \u003e\n \u003capplication\n android:allowBackup=\"true\"\n android:icon=\"@drawable/ic_launcher\"\n android:label=\"@string/app_name\"\n android:theme=\"@style/AppTheme\" \u003e\n ...\n \u003cprovider\n android:name=\"com.example.android.datasync.provider.StubProvider\"\n android:authorities=\"com.example.android.datasync.provider\"\n android:exported=\"false\"\n android:syncable=\"true\"/\u003e\n ...\n \u003c/application\u003e\n\u003c/manifest\u003e\n```\n\n\nNow that you have created the dependencies required by the sync adapter framework, you can\ncreate the component that encapsulates your data transfer code. This component is called a\nsync adapter. The next lesson shows you how to add this component to your app."]]