ContentProvider:内容提供者。她主要完成这么一件事:共享数据。将一个应用的数据共享给其他应用
代码如下:
1、SQLiteProvider
package com.njupt.practice_contentprovider1.provider;
import com.njupt.practice_contentprovider1.dao.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 SQliteProvider extends ContentProvider{
private UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
private DBOpenHelper helper;
public static final int PERSON = 1;
public static final int PERSON_ID = 2;
@Override
public boolean onCreate() {
helper = new DBOpenHelper(getContext());
matcher.addURI("jd_love_ztmm","person",PERSON);
matcher.addURI("jd_love_ztmm", "person/#", PERSON_ID);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = helper.getReadableDatabase();
switch(matcher.match(uri)){
case PERSON_ID:
long id = ContentUris.parseId(uri);
selection = selection == null ? "id = " + id : selection + " AND id = " + id;
case PERSON:
return db.query("person", null, selection, selectionArgs, null, null, sortOrder);
default:
throw new RuntimeException("Uri不能识别: " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = helper.getWritableDatabase();
switch(matcher.match(uri)){
case PERSON:
long id = db.insert("person", "id", values);
return ContentUris.withAppendedId(uri, id);
default:
throw new RuntimeException("Uri不能识别: " + uri);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = helper.getWritableDatabase();
switch(matcher.match(uri)){
case PERSON_ID:
long id = ContentUris.parseId(uri);
selection = selection == null ? "id = " + id : selection + " AND id = " + id;
case PERSON:
return db.delete("person", selection, selectionArgs);
default:
throw new RuntimeException("Uri不能识别: " + uri);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = helper.getWritableDatabase();
switch(matcher.match(uri)){
case PERSON_ID:
long id = ContentUris.parseId(uri);
selection = selection == null ? "id = " + id : selection + "AND id = " + id;
case PERSON:
return db.update("person", values, selection, selectionArgs);
default:
throw new RuntimeException("Uri不能识别: " + uri);
}
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
}
2、ProviderTest
测试类
package com.njupt.pc1;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
public class ProviderTest extends AndroidTestCase{
public void testQuery(){
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://jd_love_ztmm/person");
Cursor c = resolver.query(uri,null,null,null,null);
while(c.moveToNext()){
Integer id = c.getInt(0);
String name = c.getString(1);
Integer balance = c.getInt(2);
System.out.println(new Person(id,name,balance));
}
}
public void testDelete(){
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://jd_love_ztmm/person/1");
resolver.delete(uri, null, null);
}
public void testUpdate(){
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://jd_love_ztmm/person/2");
ContentValues values = new ContentValues();
values.put("name", "刘诗诗是java程序员的女神");
values.put("balance", "65000");
resolver.update(uri, values, null, null);
}
public void testInsert(){
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://jd_love_ztmm/person");
ContentValues values = new ContentValues();
values.put("name","刘诗诗是C++程序员的女神" );
values.put("balance", "70000");
resolver.insert(uri, values);
}
}
3、DBTest
package com.njupt.practice_contentprovider1.dao;
import java.util.List;
import java.util.Random;
import com.njupt.practice_contentprovider1.domain.Person;
import android.test.AndroidTestCase;
public class DBTest extends AndroidTestCase {
public void testCreateDataBase() {
DBOpenHelper helper = new DBOpenHelper(getContext());
helper.getWritableDatabase();
}
public void testInsert() {
PersonDao dao = new PersonDao(getContext());
Person p = new Person("我爱章泽天", 55000);
dao.insert(p);
}
public void testDelete() {
PersonDao dao = new PersonDao(getContext());
dao.delete(104);
}
public void testUpdate() {
PersonDao dao = new PersonDao(getContext());
Person p = new Person(105, "章泽天爱我", 46000);
dao.update(p);
}
public void testQuery() {
PersonDao dao = new PersonDao(getContext());
Person p = dao.query(105);
System.out.println(p);
}
public void testInsertMany() {
PersonDao dao = new PersonDao(getContext());
String strs[] = new String[]{"天天","章泽天","我爱天天","天天爱我"};
for(int i = 0 ; i < 100 ; ++i){
dao.insert(new Person(strs[new Random().nextInt(50) %4]+ i , new Random().nextInt(10000)));
}
}
public void testQueryAll(){
PersonDao dao = new PersonDao(getContext());
List<Person> persons = dao.queryAll();
for(Person p : persons){
System.out.println(p);
}
}
public void testQueryCount(){
PersonDao dao = new PersonDao(getContext());
System.out.println(dao.queryCount());
}
public void testQueryPage(){
PersonDao dao = new PersonDao(getContext());
List<Person> persons = dao.queryPage(2, 20);
for(Person p : persons){
System.out.println(p);
}
}
public void testRemit(){
PersonDao dao = new PersonDao(getContext());
dao.remit(4, 3, 3000);
}
}
4、AndroidManifest.xml
在编写完ContentProvider的代码以后,别忘了在清单文件AndroidManifest.xml中进行注册
<provider android:name="com.njupt.practice_contentprovider1.provider.SQliteProvider"
android:authorities="jd_love_ztmm"
android:exported="true"/>