package cn.class3g.visitor; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.test.AndroidTestCase; import android.util.Log; public class AccessContentProviderTest extends AndroidTestCase { public void testSave() throws Throwable{ ContentResolver resolver = this.getContext().getContentResolver(); Uri insertUri = Uri.parse("content://cn.class3g.providers.personprovider/person"); ContentValues values = new ContentValues(); values.put("name", "laozhang"); values.put("age", "50"); Uri uri = resolver.insert(insertUri, values); Log.i("TAG", uri.toString()); } public void testQuery() throws Throwable{ ContentResolver resolver = this.getContext().getContentResolver(); Uri uri = Uri.parse("content://cn.class3g.providers.personprovider/person"); Cursor cursor = resolver.query(uri, null, null, null, "personid asc"); while(cursor.moveToNext()){ int personid = cursor.getInt(cursor.getColumnIndex("personid")); String name = cursor.getString(cursor.getColumnIndex("name")); Log.i("TAG", "personid="+ personid + ",name="+ name); } cursor.close(); } public void testUpdate() throws Throwable{ ContentResolver contentResolver = this.getContext().getContentResolver(); Uri updateUri = Uri.parse("content://cn.class3g.providers.personprovider/person/5"); ContentValues values = new ContentValues(); values.put("name", "蒋介石"); contentResolver.update(updateUri, values, null, null); } public void testDelete() throws Throwable{ ContentResolver contentResolver = this.getContext().getContentResolver(); Uri uri = Uri.parse("content://cn.class3g.providers.personprovider/person/5"); contentResolver.delete(uri, null, null); } }
4、测试(注意需要先将provider拥有者工程部署到设备上)
5、ContentProvider的监听器