80,351
社区成员
发帖
与我相关
我的任务
分享
package com.dzr.test;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.test.AndroidTestCase;
public class AccessContentProvierTest extends AndroidTestCase
{
public void testInsert()throws Exception
{
Uri uri = Uri.parse("content://cn.dzr.personprovider/person");
ContentResolver resolver = this.getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "laoli");
values.put("phone", "13786788939");
values.put("amount", 12212121);
resolver.insert(uri, values);
}
}
package com.example.db;
import com.example.service.DbOpenHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class PersonProvider extends ContentProvider
{
private DbOpenHelper dbOpenHelper;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PERSONS = 1;
static{
MATCHER.addURI("cn.dzr.personprovider", "person", PERSONS);
}
public boolean onCreate()
{
dbOpenHelper = new DbOpenHelper(this.getContext());
return true;
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri)
{
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values)
{
SQLiteDatabase db = this.dbOpenHelper.getWritableDatabase();
switch(MATCHER.match(uri))
{
case 1:
long rowid = db.insert("person", null, values);
// Uri insertUri = Uri.parse("content://cn.dzr.personprovider/person" + rowid);
Uri insertUri = ContentUris.withAppendedId(uri, rowid);
return insertUri;
default:
throw new IllegalArgumentException("this is an unknow Uri:" + uri);
}
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
{
// TODO Auto-generated method stub
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs)
{
// TODO Auto-generated method stub
return 0;
}
}
public class PersonProvider extends ContentProvider
{
private DbOpenHelper dbOpenHelper;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PERSONS = 1;
static{//在perosnprovider中间加了一个s
MATCHER.addURI("cn.dzr.personsprovider", "person", PERSONS);
}
public boolean onCreate()
{
dbOpenHelper = new DbOpenHelper(this.getContext());
return true;
}
public class AccessContentProvierTest extends AndroidTestCase
{
public void testInsert()throws Exception
{
//我在此处修改成错误参数后,系统则会提示如下错误。
Uri uri = Uri.parse("content://cn.dzzr.personprovider/person");
ContentResolver resolver = this.getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "laoli");
values.put("phone", "13786788939");
values.put("amount", 12212121);
resolver.insert(uri, values);
}
}
错误提示信息:
java.lang.IllegalArgumentException: Unknown URL content://cn.dzzr.personprovider/person
at android.content.ContentResolver.insert(ContentResolver.java:862)
at com.dzr.test.AccessContentProvierTest.testInsert(AccessContentProvierTest.java:21)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
android:exported
Whether the content provider is available for other applications to use:
true: The provider is available to other applications. Any application can use the provider's content URI to access it, subject to the permissions specified for the provider.
false: The provider is not available to other applications. Set android:exported="false" to limit access to the provider to your applications. Only applications that have the same user ID (UID) as the provider will have access to it.
The default value is "true" for applications that set either android:minSdkVersion or android:targetSdkVersion to "16" or lower. For applications that set either of these attributes to "17" or higher, the default is "false".
You can set android:exported="false" and still limit access to your provider by setting permissions with the permission attribute.
android文档里的内容。若你想暴露provider给其他应用程序,设置为true.反之为false。大体是这个意思,