Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

android - SyncAdapter without a ContentProvider

I want to implement a SyncAdapter for a content I want to synchronize with a server. It seems that to do so, you need a ContentProvider registered for the authority you specify in the SyncAdapter XML property file.

As I don't want this content to be accessible to the rest of the phone, I haven't implemented my own ContentProvider and used a personal implementation to store this content.

Do you know if it is possible to provide a synchronization using a SyncAdapter without providing a ContentProvider?

Thank you very much.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You always have to specify a content provider when implementing a SyncAdapter, but that's not to say it actually has to do anything.

I've written SyncAdapters that create accounts and integrate with the "Accounts & sync" framework in Android that don't necessarily store their content in a standard provider.

In your xml/syncadapter.xml:

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" 
    android:accountType="com.company.app"
    android:contentAuthority="com.company.content"
    android:supportsUploading="false" />

In your manifest:

<provider android:name="DummyProvider"
    android:authorities="com.company.content"
    android:syncable="true"
    android:label="DummyProvider" />   

And then add a dummy provider that doesn't do anything useful except exist, DummyProvider.java:

public class DummyProvider extends ContentProvider {

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
         return 0;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public boolean onCreate() {
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                    String[] selectionArgs) {
        return 0;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...