Android Content Provider(內容提供者)

一、簡介

定義內容提供者可以開發自己已用的數據操作給第三方調用,下面的示例中用到了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 + " ");
			}
		}
	}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章