Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Content Provider(內容提供者)

Android Content Provider(內容提供者)

編輯:關於Android編程

一、簡介

定義內容提供者可以開發自己已用的數據操作給第三方調用,下面的示例中用到了Android test case ,主要是為了給數據庫中添加數據方便內容提供者讀取。

二、Test case 添加DB數據

1、聲明單元測試

    
    
        
    
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中聲明內容提供者

        
        

四、讀取內容提供者的數據

	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 + " ");
			}
		}
	}



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved