两个程序A和B,
A程序中有两个类,一个继承Activity,一个继承ContentProvider
B程序中有一个类,继承 Activity
在A程序中创建一个数据库,数据库中创建两张表,
实现B程序访问A程序数据库中的数据
A程序中继承Activity的类:创建数据库,在数据库中创建表,在表中插入数据
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中注册
<provider android:name="MyContentProvider"
android:authorities="cn.mrzhu.test24"></provider>
android:authorities="cn.mrzhu.test24"为程序B访问提供uri,只有通过此uri程序B才能访问到A程序中的数据
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程序的数据
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));
}
}
}