Android Content Provider(内容提供者)

时间:2022-07-16 09:19:52

一、简介

定义内容提供者可以开发自己已用的数据操作给第三方调用,下面的示例中用到了Android test case ,主要是为了给数据库中添加数据方便内容提供者读取。

二、Test case 添加DB数据

1、声明单元测试

 

    <instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.am.cpv" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner" />
</application>
2、创建数据库工具类SQLiteOpenHelper

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBOpenHelper extends SQLiteOpenHelper {
public MyDBOpenHelper(Context context) {
super(context, "person.db", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person (id integer primary key autoincrement,name varchar(20),number varchar(20))");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
3、创建AndroidTestCase,添加数据到数据库

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

import com.am.cpv.db.MyDBOpenHelper;

public class AddDataTestCase extends AndroidTestCase {
public void testAdd() {
MyDBOpenHelper oh = new MyDBOpenHelper(getContext());
SQLiteDatabase wdb = oh.getWritableDatabase();
// wdb.execSQL("insert into person(name) values(?)", new Object[]{"张三"});
for (int i = 0; i < 30; i++) {
ContentValues values = new ContentValues();
values.put("name", "name" + i);
values.put("number", "110-" + i);
wdb.insert("person", null, values);
}
wdb.close();
}

public void testQuery() {
MyDBOpenHelper oh = new MyDBOpenHelper(getContext());
SQLiteDatabase wdb = oh.getWritableDatabase();
// Cursor cursor = wdb.rawQuery("select * from person ", new String[] {});
Cursor cursor2 = wdb.query("person", null, null, new String[] {}, null, null, null);
while (cursor2.moveToNext()) {
String[] columnNames = cursor2.getColumnNames();
for (String colName : columnNames) {
int columnIndex = cursor2.getColumnIndex(colName);
String val = cursor2.getString(columnIndex);
System.out.print(val + " ");
}
System.out.println();
}
}
}

三、创建内容提供者

1、创建ContentProvider

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import com.am.cpv.db.MyDBOpenHelper;

public class PersonProvider extends ContentProvider {
private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private MyDBOpenHelper helper;
private static final int QUERY = 1;
static {
uriMatcher.addURI("com.am.persionProvider", "query", QUERY);
}

@Override
public boolean onCreate() {
helper = new MyDBOpenHelper(getContext());
return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
// 判断uri的请求是否为查询
if (uriMatcher.match(uri) == QUERY) {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
return cursor;
} else {
return null;
}
}
//下边的代码未作实现
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
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 int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}

}

2、AndroidManifest.xml中声明内容提供者

        <provider
android:name="com.am.cpv.pv.PersonProvider"
android:authorities="com.am.persionProvider"
android:exported="true" >
</provider>

四、读取内容提供者的数据

	public void click(View v) {
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://com.am.persionProvider/query");
Cursor cursor = cr.query(uri, null, "name=?", new String[] { "name1" }, null);
while (cursor.moveToNext()) {
String[] columnNames = cursor.getColumnNames();
for (String colName : columnNames) {
int columnIndex = cursor.getColumnIndex(colName);
String val = cursor.getString(columnIndex);
System.out.print(val + " ");
}
}
}