使用sync adapte(三)创建一个提供器

Sync adapter框架是被设计和设备数据工作,这些设备数据由灵活的和高安全的内容提供器管理。由于这个原因,sync Adapter框架期望使用这个框架的app已经为他的本地数据定义了内容提供器。如果sync Adapter框架尝试运行sync Adapter,并且我们的app没有内容提供器,我们的sync Adapter崩溃。

如果我们在开发一个新的app从服务器传输数据到设备上,我们应该强烈考虑保存本地数据在内容提供器。除了他们对于sync Adapter的重要性,内容提供器提供器一系列安全利益并且是在安卓系统上专门设计处理数据存储的。

然而,如果我们已经保存本地数据以另一种形式,我们仍然可以使用一个sync Adapter来处理数据传输。为了满足sycn Adapter框架对一个内容提供器的要求,添加一个stub内容提供器给我们的app。一个stub提供器实现内容提供器类,但是他所以要求地方方法返回null或0,。如果我们添加一个stub提供器,然后可以使用一个sync Adapter从任何我们选择的数据保存机制传输数据。

如果已经有了一个内容提供器,不需要一个stub内容提供器。在这种情况下,可以跳过直接看create a Sync Adapter。如果没有内容提供器,这节课就是教我们如何添加。

Add a Stub Content Provider

To create a stub content provider for your app, extend the class ContentProvider and stub out its requiredmethods. The following snippet shows you how to create the stub provider:

/*
 * 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;
    }
}

 

Declare the Provider in the Manifest

The sync adapter framework verifies that your app has a content providerby checking that your app has declared a provider in its app manifest. Todeclare the stub provider in the manifest, add a <provider> element with the followingattributes:

android:name="com.example.android.datasync.provider.StubProvider"

Specifies thefully-qualified name of the class that implements the stub content provider.

android:authorities="com.example.android.datasync.provider"

A URI authoritythat identifies the stub content provider. Make this value your app's packagename with the string ".provider" appended to it. Even though you'redeclaring your stub provider to the system, nothing tries to access theprovider itself.

android:exported="false"

Determines whetherother apps can access the content provider. For yourstub content provider, set the value to false, since there's no need toallow other apps to see the provider. This value doesn't affect the interactionbetween the sync adapter framework and the content provider.

android:syncable="true"

Sets a flag that indicates that the provider is syncable.If you set this flag to true, you don't have tocall setIsSyncable() in your code. The flag allows the sync adapter framework to make datatransfers with the content provider, but transfers only occur if you do themexplicitly.

你可能感兴趣的:(使用sync adapte(三)创建一个提供器)