从自定义内容提供商处访问数据

时间:2016-05-14 06:32:02

标签: android android-contentprovider

我有两个基于内容提供商的应用A和B.两者都有自己的内容提供商,并且设置用于从A和B读取数据,反之亦然。当其他应用程序处于后台时,一切正常。但是,如果应用程序被杀或在后台不存在,则无法找到其他内容提供商。例如,应用B想要从应用A中读取数据。' B'可以从A' A'成功的时候' A'在后台运行,但是如果' A'给出了致命的错误(匹配uri未找到)没有在后台运行。

有什么想法吗?

[编辑] 我收到与this post相同的问题。 我在两个应用程序中都有这个'清单:

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

这是我得到的错误:

  

将包裹写入例外                                                                               java.lang.IllegalArgumentException:不支持的URI(查询):content://com.example.appA-provider/appA                                                                                   在com.example.provider.MyContentProvider.query(MyContentProvider.java:142)                                                                                   在android.content.ContentProvider.query(ContentProvider.java:1007)                                                                                   在android.content.ContentProvider $ Transport.query(ContentProvider.java:218)                                                                                   在android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)                                                                                   在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;
    }
}

3 个答案:

答案 0 :(得分:0)

我在内容提供商处看不到init()的来电。它是否仅作为应用程序启动的一部分从其他地方调用?

如果是这样,可以解释为什么内容提供程序在应用程序尚未启动时失败:在这种情况下,UriMatcher为空,switch方法中的query()会回退到default女巫会抛出IllegalArgumentException

您应该在init()中致电onCreate()或在静态初始化程序中完全初始化UriMatcher

答案 1 :(得分:0)

您无法从代码中的其他位置初始化内容提供程序,因为ContentProvider可能是您实例化的应用程序的第一个(或唯一的)组件。

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

答案 2 :(得分:0)

您将权限设置为

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

因此,您的mAuthority可以是com.example.providercom.example.provider.myprovider

但是,您已将清单中的权限定义为

android:authorities="com.example.${applicationId}-provider"

com.example.appA-provider