且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我的Andr​​oid的ContentProvider找不到ContentResolver的

更新时间:2022-10-15 09:58:39

 <供应商的android:NAME =。LogServiceProvider
          机器人:当局=de.xyz.android.log4android
          机器人:出口=真/>

应该有所帮助:)

My goal is writing a ContentProvider without an Activity. For testing I wrote a test - activity in a own app. For those who want to tell me that there is still a logger included in android, I know this.

Here is part of the 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

So I also add the Content Provider Information to 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>

Here Is the part out of my Client activity, that is in a seperate app.

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 tells me that TestClient cant find the ContentProvider

05-27 12:42:52.099: ERROR/ActivityThread(548): Failed to find provider info for de.xyz.android.log4android

Where is my error. In my opinion I did all mentioned in: Content Providers | Android. It would be nice if you could give me an hint. If you need more code fragments to understand ask me.

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

should help :)