我遇到了这样的问题:
我编写了一个将其数据与服务器同步的应用程序。因此,我有一个服务,一个同步适配器和一个内容提供程序,可以执行所有数据库操作。

问题在于,永远不会调用内容提供程序中的重写函数(但是提供程序创建成功)。当我像

Cursor cur = mContentResolver.query(Uri.parse(URI_TO_SYNC + tableName), null, null, null, null);


它不输入提供程序的query函数,而仅返回null
我做错了什么?

清单中的声明如下所示:

    <service android:name="EuclidSyncService"
        android:exported="true">
        <intent-filter>
            <action android:name="android.content.SyncAdapter" />
        </intent-filter>
        <meta-data android:name="android.content.SyncAdapter"
            android:resource="@xml/euclid_sync" />
    </service>

    <provider
        android:name="EuclidSyncProvider"
        android:authorities="com.android.radiusz.euclid.sync"
        android:exported="false"
        android:enabled="true">

    </provider>


euclid_sync.xml文件包含sync-adapter声明:

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.android.radiusz.euclid.sync"
    android:accountType="com.radiusz.euclid.account"
    android:userVisible="false"
    android:supportsUploading="true"
    android:allowParallelSyncs="false"
    android:isAlwaysSyncable="true"
/>


内容提供商代码:

package com.android.radiusz.euclid.syncadapter;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class EuclidSyncProvider extends ContentProvider {
private SyncDatabaseHelper dbHelper;
private SQLiteDatabase db;
private static final String AUTHORITY = "com.android.radiusz.euclid.sync";
private static final int QUERY=0;

@Override
public boolean onCreate() {
    Log.d("CONTENT PROVIDER","ONCREATE");
    dbHelper=new SyncDatabaseHelper(getContext());
    db=dbHelper.getWritableDatabase();
    return true;
}

private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
    sUriMatcher.addURI(AUTHORITY, "/*", QUERY);
}
@Override
public String getType(Uri u) {
    final int match = sUriMatcher.match(u);
    switch (match) {
        case QUERY:
            Log.d("gettype", "GETTYPE");
            return ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.euclidsyncadapter.entries";
        default:
            throw new UnsupportedOperationException("Unknown uri: " + u);
    }
}

@Override
public Cursor query(
        Uri uri,
        String[] projection,
        String selection,
        String[] selectionArgs,
        String sortOrder) {
    String tableName=uri.getLastPathSegment();
    Log.d("TABLENAME", tableName);
    return db.query(true,tableName,projection,selection,selectionArgs,null,null,sortOrder,null);
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    String tableName=uri.getLastPathSegment();
    db.insert(tableName,null,values);
    return null;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    String tableName=uri.getLastPathSegment();
    return db.delete(tableName,selection,selectionArgs);
}

public int update(
        Uri uri,
        ContentValues values,
        String selection,
        String[] selectionArgs) {
    String tableName=uri.getLastPathSegment();
    return db.update(tableName,values,selection,selectionArgs);
}

public int truncate( String table ){
    return 0;
}
}

最佳答案

通过用ContentResolver替换ContentProviderClient解决了问题

10-08 03:38