需求 :有兩個應用程序分別是com.itheima.contentProvider和com.itheima.otherApp,
在com.itheima.contentProvider中有個數據庫info.db,在數據庫中有張表person.需要在otherApp中獲得person表中的數據,並且對person表中的數據進行增刪改查.
思路 : 要想在其他應用程序中調用本程序中的增,刪,改,查方法,那麼首先在程序中應該有個ContentProvider類,在該類中定義好增刪改查的函數.當otherApp調用 contentProvider中的增刪改查方法時,要先發送個Uri來進行判斷,如果Uri匹配成功的話,那麼就繼續執行下面的代碼,以此完成對數據庫的增刪改查.
重點 : 要掌握的主要是otherApp中的思路.因爲在實際的開發中,基本上,都是我們寫程序來調用其他應用的ContentProvider,來完成對其他應用的增刪改查,如: 對獲得系統短信的功能就是在調用系統級應用短信中的ContentProvider的增刪改查的方法.
- - - - - - - - -- - - - - - -- - - - - - - - - - - - - - - -- - - - - - -- - - - - - - 先完成com.itheima.contentProvider應用- - - - - - - - -- - - - - - -- - - - - - - - - - - - - - - -- - - - - - -- - - - - - - - - - - - - - - -- - - -
步驟 : 1,,在com.itheima.contentProvider中定義一個類PersonContentProvider繼承ContentProvider,並重寫insert,delete,update,query幾個函數.
2,在PersonContentProvider類中,定義多個Uri,並且添加到Uri匹配器中,當otherApp發送Uri來時,就用該匹配器中的Uri來進行匹配,以此判斷Uri是否正確.
3,爲每個Uri定義好一個匹配成功的碼,當進行插入操作時,匹配成功的話,返回對應的成功碼,根據該成功碼進行插入操作
* 當進行修改操作時,匹配成功的話,返回相應的成功碼,根據該成功碼進行修改操作.
4,因爲,Uri的添加要在很早就要完成,所以,乾脆將匹配器的創建和Uri的添加放在靜態代碼塊中,在該類加載時就完成這些操作.
5,在com.itheima.contentProvider的應用中的清單文件中的和Activity同一級節點下:
<provider
android:name="com.itheima.contentProvider.provider.PersonContentProvider"
android:exported="true"
android:authorities="com.itheima.contentProvider.provider.PersonContentProvider"
>
注意 : 1,在android4.2以後,默認的內容提供者是不對外暴露數據的,這裏就要將其設置爲:android:exported="true"
2,在有的模擬器下android:authorities=".provider.PersonContentProvider"這樣寫沒錯.
但是,在我的模擬器下必須寫全稱 ,既 android:authorities=“com.itheima.contentProvider.provider.PersonContentProvider”
com.itheima.contentProvider應用中的PerosnContentProvider類中的代碼:
public class PersonContentProvider extends ContentProvider {
static UriMatcher uriMatcher = null;
//定義一條插入操作的uri
static String authority = "com.itheima.contentProvider.provider.PersonContentProvider";//定義插入操作的uri的主機名,這個uri是用來添加到匹配器中的
static int person_insert_code = 1000;//插入操作的uri匹配成功的返回碼
static int person_delete_code = 1001;//刪除操作的uri匹配成功的返回碼
static int person_update_code = 1002;//修改操作的uri匹配成功的返回碼
static int person_queryAll_code = 1003;//查詢全部的操作的uri匹配成功的返回碼
static int person_queryItem_code = 1004;//查詢單條操作的uri匹配成功的返回碼
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);//在後面進行匹配時,如果匹配失敗,返回NO_MATCH碼
//該插入操作的uri的完整形式是 : content://com.itheima.contentProvider.provider.PersonContentProvider/person/insert
uriMatcher.addURI(authority, "person/insert", person_insert_code);//將插入操作的uri以及匹配成功碼添加到匹配器中,爲的就是在後面和傳進來的進行插入操作的uri進行匹配判斷
//該修改操作的uri的完整形式是 : content://com.itheima.contentProvider.provider.PersonContentProvider/person/delete
uriMatcher.addURI(authority, "person/delete", person_delete_code);///將修改操作的uri以及匹配成功碼添加到匹配器中
//uri的完整形式 : com.itheima.contentProvider.provider.PersonContentProvider/person/update
uriMatcher.addURI(authority, "person/update", person_update_code);
//因爲是查詢全部,所以完整的uri是 : com.itheima.contentProvider.provider.PersonContentProvider/person
uriMatcher.addURI(authority, "person", person_queryAll_code);
//因爲是查詢單條,所以完整的uri是 : com.itheima.contentProvider.provider.PersonContentProvider/person/#,必須要用佔位符代替
uriMatcher.addURI(authority, "perosn/#", person_queryItem_code);
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext(), "info.db");
SQLiteDatabase db = openHelper.getReadableDatabase();
Cursor cursor = null;
switch(uriMatcher.match(uri))
{
case 1003://匹配成功的話,並且返回的成功碼是1003就走這條路,查詢表中的全部信息
cursor = db.query("person", null, null, null, null, null, null);
break;
case 1004://匹配成功的話,並且返回的成功碼是1004就走這條路,查詢單條語句
cursor = db.query("perosn", null, selection, selectionArgs, null, null, null);
break;
default :
throw new RuntimeException("傳入的Uri不匹配");
}
return cursor;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
if(uriMatcher.match(uri) == 1000)//傳進來的Uri如果匹配的話,就會返回這個成功碼 1000
{
//開始往數據庫的表中插入一條數據
SQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext(), "info.db");
SQLiteDatabase db = openHelper.getWritableDatabase();
db.insert("person", null, values);
System.out.println("插入數據成功!");
db.close();
}else{
throw new RuntimeException("Uri不匹配");
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
if(uriMatcher.match(uri) == 1001)//傳進來的Uri如果匹配的話,就會返回這個成功碼 1001
{
//開始從數據庫中刪除一條或多條數據
SQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext(), "info.db");
SQLiteDatabase db = openHelper.getWritableDatabase();
int i = db.delete("person", selection, selectionArgs);
System.out.println("刪除成功");
db.close();
return i ;
}else{
throw new RuntimeException("傳入的Uri不匹配");
}
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
if(uriMatcher.match(uri) == 1002)//傳進來的Uri如果匹配的話,就會返回這個成功碼 1002
{
//開始從數據庫中修改一條或多條數據
SQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext(), "info.db");
SQLiteDatabase db = openHelper.getWritableDatabase();
int i = db.update("person", values,selection ,selectionArgs);
db.close();
return i;
}else{
throw new RuntimeException("傳入的Uri不匹配");
}
}
}
- - - - - - - - -- - - - - - -- - - - - - - - - - - - - - - -- - - - - - -- - - - - - - 再完成com.itheima.otherApp應用- - - - - - - - -- - - - - - -- - - - - - - - - - - - - - - -- - - - - - -- - - - - - - - - - - - - - - -- - - - - - - -
* 2,準備好Uri對象,用來導引到ContentProvider應用中去.
在com.itheima.otherApp應用中的Test類中的代碼 :
public class Test extends AndroidTestCase {
//測試往ContentProvider應用中的數據庫中的表中插入一條數據
public void insertTest(){
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://com.itheima.contentProvider.provider.PersonContentProvider/person/insert");
ContentValues values = new ContentValues();
values.put("name", "男模");
values.put("age", 22);
resolver.insert(uri, values);
}
//測試從ContentProvider應用中的數據庫中的表中刪除一條或多條數據
public void deleteTest()
{
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://com.itheima.contentProvider.provider.PersonContentProvider/person/delete");
int i = resolver.delete(uri,"id > ?", new String []{"7"});
System.out.println("刪除了 "+i +" 行");
}
//測試修改ContentProvider應用中的數據庫中的表中的一條數據
public void updateTest()
{
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://com.itheima.contentProvider.provider.PersonContentProvider/person/update");
ContentValues values = new ContentValues();
values.put("name", "張胖子");
values.put("age", 20);
int i = resolver.update(uri, values, "name = ?", new String [] {"胡漢三"});
System.out.println("修改了 "+ i + " 行");
}
//測試查詢ContentProvider應用中的數據庫中的表中的所有數據
public void queryAll()
{
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://com.itheima.contentProvider.provider.PersonContentProvider/person");
Cursor cursor = resolver.query(uri, null, null, null, null);
if(cursor != null)
{
while(cursor.moveToNext())
{
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
System.out.println("id : "+ id+" name : "+name +" age : "+age);
}
}
cursor.close();
}
//測試查詢ContentProvider應用中的數據庫中的表中的一條數據
public void queryItem()
{
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://com.itheima.contentProvider.provider.PersonContentProvider/person/#");
Cursor cursor = resolver.query(uri, null, "id = ?", new String[] {"1"}, null);
if(cursor.moveToFirst())
{
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
System.out.println("id : "+ id+" name : "+name +" age : "+age);
}
}
}