1】、由于在android开发中需要对外共享一些数据,比如通讯,照片等等。Android的数据存储方式总共有五种,分别是:Shared Preferences、网络存储、文件存储、外储存储、SQLite。但是我们知道一般这些存储都只是在单独的一个应用程序之中达到一个数据的共享,而且这些知识在前面我都有介绍,有时候我们需要操作其他应用程序的一些数据,例如我们需要操作系统里的媒体库、通讯录等,这时我们就可能通过ContentProvider来满足我们的需求了ContentProvider就是用来共享数据。
2】、要实现ContentProvider共享需要继承ContentProvider类,重写里面所自己需要的方法。还需要在配置文件中
<application>
<provider android:name="类的路径" android:authorities="共享ID"/>
</application>
3】、代码实例:
//创建SQLite数据库信息
package com.example.servier;
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper { public DBOpenHelper(Context context) { super(context, "itcast.db", null, 2); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("create table person(p_id integer primary key autoincrement, name varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("alter table person add phone varchar(11) null"); } }
//创建ContentProvider共享实例
package com.example.batabase;
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; import android.util.Log; import com.example.servier.DBOpenHelper; public class PersonProvider extends ContentProvider { private DBOpenHelper openhelper; private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); //匹配默认值 private static final int PERSONS = 1; private static final int PERSON = 2; // 前面使用构造方法创建对象,需要使用静态方法放入静态池中! static { MATCHER.addURI("cn.itcast.providers.personprovider", "person", PERSONS); //增加访问信息,person为表名,PERSONS为匹配返回值 MATCHER.addURI("cn.itcast.providers.personprovider", "person/#", PERSON); } @Override public boolean onCreate() { openhelper = new DBOpenHelper(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = openhelper.getReadableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: return db.query("person", projection, selection, selectionArgs, null, null, sortOrder); case PERSON: long rowid = ContentUris.parseId(uri); String where = "p_id=" + rowid; if (selection != null && !"".equals(selection.trim())) { where += " and " + selection; } return db.query("person", projection, where, selectionArgs, null, null, sortOrder); default: throw new IllegalArgumentException("this is Unknown Uri:" + uri); } } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = openhelper.getWritableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: long rewid = db.insert("person", "name", values); Uri appuid = ContentUris.withAppendedId(uri, rewid); this.getContext().getContentResolver().notifyChange(uri, null); return appuid; default: throw new IllegalArgumentException("This is no kown:" + uri); } } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = openhelper.getReadableDatabase(); int num = 0; switch (MATCHER.match(uri)) { case PERSONS: num = db.delete("person", selection, selectionArgs); break; case PERSON: long p_id = ContentUris.parseId(uri); String where = "p_id=" + p_id; if (selection != null && !"".equals(selection.trim())) { where = "and " + selection; } num = db.delete("person", where, selectionArgs); break; default: throw new IllegalArgumentException("This is no kown:" + uri); } return num; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = openhelper.getReadableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: return db.update("person", values, selection, selectionArgs); case PERSON: long p_id = ContentUris.parseId(uri); String where = "p_id=" + p_id; if (selection != null && !"".equals(selection.trim())) { where = "and " + selection; } return db.update("person", values, where, selectionArgs); default: throw new IllegalArgumentException("This is no kown:" + uri); } } }
//测试用例
package com.logtest;
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 PersonProviderTest extends AndroidTestCase {
private String log="PersonProviderTest";
public void testInsert() throws Exception{
Uri uri = Uri.parse("content://cn.itcast.providers.personprovider/person");
ContentResolver resolver = this.getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "laoli");
values.put("phone", "1860103838383");
resolver.insert(uri, values);
}
public void testQuery() throws Exception{
Uri uri = Uri.parse("content://cn.itcast.providers.personprovider/person");
ContentResolver resolver = this.getContext().getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, "p_id asc");
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.i(log, name);
}
cursor.close();
}
public void testDelete() throws Exception{
Uri uri = Uri.parse("content://cn.itcast.providers.personprovider/person/3");
ContentResolver resolver = this.getContext().getContentResolver();
resolver.delete(uri, null, null);
}
public void testUpdate() throws Exception{
Uri uri = Uri.parse("content://cn.itcast.providers.personprovider/person/4");
ContentResolver resolver = this.getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "miku12301212312312323");
resolver.update(uri, values,null , null);
}
}