2

我的目标是编写一个没有 Activity 的 ContentProvider。为了测试,我在自己的应用程序中编写了一个测试活动。对于那些想告诉我android中仍然包含一个记录器的人,我知道这一点。

这是 ContentProvider 的一部分

public static final String AUTHORITY = "de.xyz.android.log4android";
public static final int LOGGER_ARROW_URI_ID = 1;
public static final String ARROW_CONTENT_TYPE = 
    "vnd.de.xyz.android.cursor.dir/vnr.log";

public static final int LOGGER_ITEM_URI_ID = 2;
public static final String ITEM_CONTENT_TYPE = 
    "vnd.de.xyz.android.cursor.item/vnr.log";

public static final UriMatcher sUriMatcher;
static {
    sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    sUriMatcher.addURI(AUTHORITY, LOGGER_TABLE, LOGGER_ARROW_URI_ID);
    sUriMatcher.addURI(AUTHORITY, LOGGER_TABLE + "#", LOGGER_ITEM_URI_ID);
}

public static final Uri CONTENT_URI = Uri.parse("content://"
        + LogServiceProvider.AUTHORITY + "/logs");

public static final DefaultLogEntry LOG_ENTRY = null;

//Some more not important code

@Override
public Uri insert(Uri uri, ContentValues initialValues) {
    synchronized (this) {
        try {
            long rowID = dbHelper.getWritableDatabase().insert(LOGGER_TABLE,
                    null, initialValues);

            if (rowID > 0) {
                Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
                getContext().getContentResolver().notifyChange(_uri, null);
                return _uri;
            }
        } catch (Exception ex) {
        }
        throw new SQLException("Failed to insert row into " + uri);
    }
}

//Some more not important code

所以我还将内容提供者信息添加到 manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="de.xyz.android.logger"
          android:versionCode="1"
          android:versionName="1.0">

    <provider 
        android:name="de.xyz.android.logger.LogServiceProvider" 
        android:authorities="de.xyz.android.log4android"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-sdk android:minSdkVersion="4" /> 
</application>

这是我的客户活动的一部分,它在一个单独的应用程序中。

ContentResolver cr = getContentResolver();
Uri getItem = Uri.parse("content://de.xyz.android.log4android/logs");
ContentValues values = new ContentValues();
values.put("level","WARNING");
values.put("time","1986-11-16");
values.put("message","FistTest");
values.put("owner","jsb");
values.put("file","testLog.java");
values.put("line","27");
Uri newItem = cr.insert(getItem, values);

LogCat 告诉我 TestClient 找不到 ContentProvider

05-27 12:42:52.099: 错误/ActivityThread(548): 无法找到 de.xyz.android.log4android 的提供者信息

我的错误在哪里。在我看来,我确实在以下内容中提到了所有内容:Content Providers | 安卓。如果您能给我一个提示,那就太好了。如果您需要更多代码片段来理解,请询问我。

4

2 回答 2

4
<provider android:name=".LogServiceProvider"
          android:authorities="de.xyz.android.log4android"
          android:exported="true" />

应该有帮助:)

于 2011-05-27T13:50:16.087 回答
1

这可能会失败的一种深奥方式是,如果您在内容提供者上定义了一个带有参数的构造函数。这将导致默认的 0 参数构造函数不存在。这会导致提供程序的运行时注册失败,因为系统在清单中的 android:name 下指定的类中找不到零参数构造函数。

于 2011-10-06T06:34:36.010 回答