兩個程序A和B,
A程序中有兩個類,一個繼承Activity,一個繼承ContentProvider
B程序中有一個類,繼承 Activity
在A程序中創建一個數據庫,數據庫中創建兩張表,
實現B程序訪問A程序數據庫中的數據
A程序中繼承Activity的類:創建數據庫,在數據庫中創建表,在表中插入數據
[html]
package cn.mrzhu.test24;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
public class Provider extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//創建數據庫
SQLiteDatabase db = openOrCreateDatabase("test24.db", MODE_PRIVATE, null);
//創建表
db.execSQL("create table test1 (_id integer, name varchar(20), age integer)");
db.execSQL("create table test2 (_id integer, name varchar(20), age integer)");
//給test1表添加數據
ContentValues values = new ContentValues();
values.put("name", "zhangsan");
values.put("age", 30);
db.insert("test1", "name", values);
values.clear();
values.put("name", "lisi");
values.put("age", 40);
db.insert("test1", "name", values);
//給test2表添加數據
values.clear();
values.put("name", "Peter");
values.put("age", 20);
db.insert("test2", "name", values);
values.clear();
values.put("name", "Mike");
values.put("age", 25);
db.insert("test2", "name", values);
}
}
A程序中繼承ContentProvider的類:需在AndroidManifest.xml中注冊
[html]
<provider android:name="MyContentProvider"
android:authorities="cn.mrzhu.test24"></provider>
android:authorities="cn.mrzhu.test24"為程序B訪問提供uri,只有通過此uri程序B才能訪問到A程序中的數據
[html]
package cn.mrzhu.test24;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MyContentProvider extends ContentProvider{
//Uri過濾器,通過此過濾器可以取出uri最後一個字段
UriMatcher um;
static final String AUTHORITY = "cn.mrzhu.test24";
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
//初始化過濾器
um = new UriMatcher(UriMatcher.NO_MATCH);
//添加過濾內容,從其他程序傳來的uri通過過濾器之後若其字段為test1,則其code為1
um.addURI(AUTHORITY, "test1", 1);
um.addURI(AUTHORITY, "test2", 2);
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = getContext().openOrCreateDatabase("test24.db", Context.MODE_PRIVATE, null);
Cursor cs = null;
//將uri過濾,取得返回值
int code = um.match(uri);
//根據不同的返回值執行不同的查詢語句
switch(code){
case 1:
cs = db.rawQuery("select * from test1", null);
break;
case 2:
cs = db.rawQuery("select * from test2", null);
break;
}
//執行查詢語句後將結果集返回
return cs;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
}
B程序:通過A程序中繼承ContentProvider的類提供的uri訪問到A程序的數據
[html]
package cn.mrzhu.test24x;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得ContentResolver對象
ContentResolver cr = getContentResolver();
//指定uri並指定操作哪個表
Uri uri = Uri.parse("content://cn.mrzhu.test24/test1");
//執行query方法返回一個結果集
Cursor cs = cr.query(uri, null, null, null, null);
//遍歷結果集,取出數據
while(cs.moveToNext()){
Log.i("System.out", "_id" + cs.getInt(0) + "name" + cs.getString(1) + "age" + cs.getInt(2));
}
}
}