创建桩内容提供器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
同步适配器框架旨在处理由 柔性环境
高度安全的内容提供程序框架因此,同步适配器框架需要
使用该框架的应用已为其本地数据定义了 content provider。
如果同步适配器框架尝试运行您的同步适配器,但您的应用
content provider,您的同步适配器将会崩溃。
如果您正在开发将数据从服务器传输到设备的新应用,您应该
强烈建议将本地数据存储在 content provider 中。它们不仅对于
同步适配器、Content Provider 提供各种安全优势,
用于处理 Android 系统上的数据存储。详细了解如何创建内容
provider,请参阅创建 content provider。
不过,如果您已经以其他形式存储本地数据,则仍然可以使用同步功能,
适配器来处理数据传输。为了满足同步适配器框架对
content provider,请向您的应用添加桩 content provider。桩提供器用于实现
content provider 类,但其所有必需的方法都会返回 null
或 0
。如果您
添加桩提供器,然后使用同步适配器从任何存储空间
您选择的机制
如果您的应用中已经有内容提供器,则不需要桩内容提供器。
在这种情况下,您可以跳过本课程,继续学习
创建同步适配器。如果您还没有
本课将介绍如何添加桩内容提供器
将同步适配器插入框架中。
添加桩内容提供器
要为您的应用创建桩内容提供器,请扩展 类
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;
}
}
在清单中声明提供器
同步适配器框架通过检查
应用在其应用清单中声明了提供程序。要在
清单中,请添加具有以下属性的 <provider>
元素:
-
android:name="com.example.android.datasync.provider.StubProvider"
-
指定实现桩内容提供程序的类的完全限定名称。
-
android:authorities="com.example.android.datasync.provider"
-
用于标识桩内容提供器的 URI 授权。将此值设置为您的应用
带有“.provider”字符串的软件包名称。即使您声明了
将桩提供程序添加到系统,系统不会尝试访问该提供程序本身。
-
android:exported="false"
-
确定其他应用是否可以访问 content provider。桩内容
提供程序时,请将该值设置为
false
,因为无需允许其他应用查看
。此值不会影响同步适配器框架之间的交互
以及内容提供程序
-
android:syncable="true"
-
设置指示提供程序可同步的标记。如果您将此标志设置为
true
时,您无需在代码中调用 setIsSyncable()
。该标记允许同步适配器框架
与内容提供者进行传输,但只有在您明确进行传输时,传输才会发生。
以下代码段展示了如何将
<provider>
元素添加到应用清单中:
<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>
现在您已经创建了同步适配器框架所需的依赖项,接下来可以
创建封装数据传输代码的组件。该组件称为
同步适配器。下一课将介绍如何向应用添加此组件。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],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."]]