且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有 ContentProvider 的 SyncAdapter

更新时间:2023-11-16 14:51:16

在实现 SyncAdapter 时,您总是必须指定内容提供程序,但这并不是说它实际上必须执行任何操作.

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

我编写了 SyncAdapters,用于创建帐户并与 Android 中的帐户与同步"框架集成,这些框架不一定将其内容存储在标准提供程序中.

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.

在您的 xml/syncadapter.xml 中:

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" />

在您的清单中:

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

然后添加一个除了存在之外没有任何用处的虚拟提供程序,DummyProvider.java:

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