gpt4 book ai didi

android - 从 Custom Content Provider 访问数据

转载 作者:行者123 更新时间:2023-12-05 07:49:33 26 4
gpt4 key购买 nike

我有两个基于内容提供程序的应用程序 A 和 B。它们都有自己的内容提供程序,并且设置为从 A 和 B 读取数据,反之亦然。当其他应用程序处于后台时,一切正常。但如果应用程序被终止或不在后台出现,则无法找到其他内容提供者。例如,应用程序 B 想从应用程序 A 读取数据。当 'A' 在后台运行时,'B' 可以从 'A' 成功读取数据,但如果 'A' 未运行,则会出现 fatal error (找不到匹配的 uri)在后台。

有什么想法吗?

[编辑]我遇到了与 this post 相同的问题.我在两个应用程序的 list 中都有:

   <provider
android:name="MyContentProvider"
android:authorities="com.example.${applicationId}-provider"
android:enabled="true"
android:exported="true"
android:grantUriPermissions="true">
</provider>

这是我遇到的错误:

Writing exception to parcel java.lang.IllegalArgumentException: Unsupported URI(Query): content://com.example.appA-provider/appA at com.example.provider.MyContentProvider.query(MyContentProvider.java:142) at android.content.ContentProvider.query(ContentProvider.java:1007) at android.content.ContentProvider$Transport.query(ContentProvider.java:218) at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) at android.os.Binder.execTransact(Binder.java:461)

注意:只有当另一个应用程序不在后台时才会发生这种情况,否则它会按预期工作(可以正常读取彼此的数据)。

[编辑 2]这是 MyContentProvider 的代码:

package com.example.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;


public class MyContentProvider extends ContentProvider {

private static DatabaseHelper dbHelper;

private static final int ALL_ENTRIES = 1;
private static final int SINGLE_ENTRY = 2;

private String mAuthority = BuildConfig.APPLICATION_ID;
private static UriMatcher uriMatcher;

public Uri CONTENT_URI= null;

static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
}

public MyContentProvider() {}

public void init(String packageName, String authority) {
if (authority == null) {
setAuthority(packageName, true);
} else {
setAuthority(authority, false);
}

uriMatcher.addURI(getAuthority(), TABLE_NAME, ALL_ENTRIES);
uriMatcher.addURI(getAuthority(), TABLE_NAME + "/#", SINGLE_ENTRY);
CONTENT_URI =
Uri.parse("content://" + getAuthority() + "/" + TABLE_NAME);
}

private void setAuthority(String packageName, boolean isPackageName) {
if (isPackageName) {
mAuthority = packageName + ".myprovider";
} else {
mAuthority = packageName;
}
}

public String getAuthority() {
return mAuthority;
}

public Uri getContentUri() {
return CONTENT_URI;
}

@Override
public boolean onCreate() {
dbHelper = new DatabaseHelper(getContext());
return false;
}

//Return the MIME type corresponding to a content URI
@Override
public String getType(Uri uri) {

if (uri == null) {
throw new IllegalArgumentException("Content uri is null: " + uri);
}
if (uriMatcher == null) {
throw new IllegalArgumentException("Unsupported Match URI: " + uri);
}

switch (uriMatcher.match(uri)) {
case ALL_ENTRIES:
return "vnd.android.cursor.dir/vnd." + getAuthority() + "." + TABLE_NAME;
case SINGLE_ENTRY:
return "vnd.android.cursor.item/vnd." + getAuthority() + "." + TABLE_NAME;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}

@Override
public Uri insert(Uri uri, ContentValues values) {
Uri _uri = null;
long id = 0;
SQLiteDatabase db = dbHelper.getWritableDatabase();
switch (uriMatcher.match(uri)) {
case ALL_ENTRIES:
case SINGLE_ENTRY:
id = db.insert(TABLE_NAME, null, values);
getContext().getContentResolver().notifyChange(uri, null);
_uri = Uri.parse(CONTENT_URI + "/" + id);
break;
default:
throw new IllegalArgumentException("Unsupported URI (insert): " + uri);
}

return _uri;
}

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

SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

Cursor cursor = null;
String id = null;

switch (uriMatcher.match(uri)) {
case ALL_ENTRIES:
queryBuilder.setTables(TABLE_NAME);
cursor = queryBuilder.query(db, projection, selection,
selectionArgs, null, null, sortOrder);
break;
case SINGLE_ENTRY:
queryBuilder.setTables(TABLE_NAME);
id = uri.getPathSegments().get(1);
if (id != null && !id.isEmpty()) {
queryBuilder.appendWhere(TABLE_NAME + "=" + id);
}

cursor = queryBuilder.query(db, projection, selection,
selectionArgs, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Unsupported URI(Query): " + uri);
}

return cursor;
}
}

最佳答案

您不能像这样从代码中的其他地方初始化内容提供程序,因为 ContentProvider 可能是应用程序中第一个(或唯一)实例化的组件。

但是,您可以从 Manifest 或 String 资源中动态读取权限。在我对 Does Android Content Provider authority definition break the DRY rule? 的回答中我在 OpenTasks-Provider 中概述了我们是如何做到这一点的。

关于android - 从 Custom Content Provider 访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37223456/

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