我的自定义 ContentProvider 总是得到一个 NULL Cursor

I always get a NULL Cursor for my custom ContentProvider

我有一个 ContentProvider 访问问题,我已将其提炼成一个小型测试驱动程序。

供应商清单声明它:

<provider
  android:name=".MyContentProvider"
  android:authorities="com.foo.provider"
  android:enabled="true"
  android:exported="true">
</provider>

我的内容提供者 class 是从 here:

的例子中提取的
package com.foo.alpha;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import java.util.HashMap;

public class MyContentProvider extends ContentProvider {
    // defining authority so that other application can access it
    static final String PROVIDER_NAME = "com.foo.provider";

    // defining content URI
    static final String URL = "content://" + PROVIDER_NAME + "/keys";

    // parsing the content URI
    static final Uri CONTENT_URI = Uri.parse(URL);

    static final int uriCode = 1;
    static final UriMatcher uriMatcher;
    private static HashMap<String, String> values;

    static {

        // to match the content URI
        // every time user access table under content provider
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

        // to access whole table
        uriMatcher.addURI(PROVIDER_NAME, "keys", uriCode);

        // to access a particular row
       // uriMatcher.addURI(PROVIDER_NAME, "keys/*", uriCode);
    }

    public MyContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int count = 0;
        switch (uriMatcher.match(uri)) {
            case uriCode:
                count = db.delete(TABLE_NAME, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        switch (uriMatcher.match(uri)) {
            case uriCode:
                return "com.foo.provider/keys";
            default:
                throw new IllegalArgumentException("Unsupported URI: " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        long rowID = db.insert(TABLE_NAME, "", values);
        if (rowID > 0) {
            Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
            getContext().getContentResolver().notifyChange(_uri, null);
            return _uri;
        }
        throw new SQLiteException("Failed to add a record into " + uri);
    }

    @Override
    public boolean onCreate() {
        Context context = getContext();
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        db = dbHelper.getWritableDatabase();
        if (db != null) {
            return true;
        }

        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(TABLE_NAME);
        switch (uriMatcher.match(uri)) {
            case uriCode:
                qb.setProjectionMap(values);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        Cursor c = qb.query(db, projection, selection, selectionArgs, null,
                null, null);
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int count = 0;
        switch (uriMatcher.match(uri)) {
            case uriCode:
                count = db.update(TABLE_NAME, values, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }


    // creating object of database
    // to perform query
    private SQLiteDatabase db;

    // declaring name of the database
    static final String DATABASE_NAME = "db";

    // declaring table name of the database
    static final String TABLE_NAME = "keys";

    // declaring version of the database
    static final int DATABASE_VERSION = 1;

    // sql query to create the table
    static final String CREATE_DB_TABLE = " CREATE TABLE " + TABLE_NAME
            + " (name TEXT PRIMARY KEY NOT NULL, keydata TEXT NOT NULL);";

    // creating a database
    private static class DatabaseHelper extends SQLiteOpenHelper {

        // defining a constructor
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        // creating a table in the database
        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(CREATE_DB_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            // sql query to drop a table
            // having similar name
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }

}

我已将一些虚拟数据插入到提供商应用程序的数据库中:

    // stuff some data into our database
    try {
        ContentValues values = new ContentValues();
        values.put("name", "gordon");
        values.put("keydata", "foobar");
        getContentResolver().insert(MyContentProvider.CONTENT_URI, values);
        Log.d("gordon", "inserted record");
    } catch (Exception e) {
        Log.d("gordon", e.toString());
    }

这一切似乎都很好。现在我有一个单独的测试应用程序试图从提供商那里查询数据。这是那个人的相关代码:

            String URL = "content://com.foo.provider/keys";
            Uri uri = Uri.parse(URL);
            String[] projection = new String[]{"name", "keydata"};
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);

Cursor 在这里总是空的。我怀疑我只是遗漏了一些简单的东西,但我不知道我不知道什么。

更新:当我尝试从第二个应用程序访问内容提供者时,我注意到 logcat 中的这个 1-liner:

AppsFilter: interaction: PackageSetting{e4410e4 com.foo.mysender/10346} -> PackageSetting{1e89e76 com.foo.alpha/10347} BLOCKED

不知道为什么被屏蔽了

原来是包可见性问题。

我偶然发现了 this,这导致我的清单中添加了以下内容:

<queries>
    <package android:name="com.foo.alpha" />
</queries>

然后成功了。