不调用ContentProvider的query()方法

时间:2014-07-02 06:02:35

标签: android android-contentresolver

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

问题是内容提供程序中的覆盖函数永远不会被调用(但是成功创建了提供程序)。当我执行像

这样的通话时
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;
}
}

1 个答案:

答案 0 :(得分:1)

通过将ContentResolver替换为ContentProviderClient

来解决问题