gpt4 book ai didi

android - ContentProvider 的 query() 方法没有被调用

转载 作者:行者123 更新时间:2023-11-30 02:49:04 27 4
gpt4 key购买 nike

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

问题在于,content provider 中的覆盖函数从未被调用(但 provider 已成功创建)。当我执行像

这样的调用时
Cursor cur = mContentResolver.query(Uri.parse(URI_TO_SYNC + tableName), null, null, null, null);

它不进入提供者的query函数,它只是返回null。我做错了什么?

list 中的声明如下所示:

    <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 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 解决了问题

关于android - ContentProvider 的 query() 方法没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24523922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com