内容提供者—Content Provider(二)

   尽管androidSDK 提供很多的内置的ContentProvider,我们有时候也要为自己开发的程序写一个自定义的ContentProvider,让别的程序可以访问到程序的数据库。想想这一件比较奇特的事情啊,我都迫不及待的看看今天的代码了,我想先把步骤一步一步写清楚,因为这个过程还是有点复杂的。

一、编写一个类继承ContentProvider

二、实现ContentProvider类的所有抽象fangfa

三、定义contentProvider的URI

四、使用UriMatcher对象映射Uri和返回代码,通常会将一个Uri映射到一个int类上。这样用起来还是比较方便的。

五、根据实际需要实现相应的方法

六、注册ContentProvider

在写程序之前先准备一个了数据库,放在了assets目录下。

package mobile.android.region.content.provider;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

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 android.util.Log;

public class RegionContentProvider extends ContentProvider
{
	private static UriMatcher uriMatcher;
	private static final String AUTHORITY = "mobile.android.regioncontentprovider";
	private static final int CITIES = 1;
	private static final int CITY_CODE = 2;
	private static final int CITY_NAME = 3;
	private static final int CITIES_IN_PROVINCE = 4;
	private SQLiteDatabase database;

	static
	{

		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(AUTHORITY, "cities", CITIES);
		uriMatcher.addURI(AUTHORITY, "code/#", CITY_CODE);
		uriMatcher.addURI(AUTHORITY, "name/*", CITY_NAME);
		uriMatcher
				.addURI(AUTHORITY, "cities_in_province/*", CITIES_IN_PROVINCE);

	}

	private SQLiteDatabase openDatabase()
	{
		try
		{
			String databaseFilename = "/sdcard/region.db";
			if (!(new File(databaseFilename)).exists())
			{
				InputStream is = getContext().getResources().getAssets()
						.open("region.db");
				FileOutputStream fos = new FileOutputStream(databaseFilename);
				byte[] buffer = new byte[8192];
				int count = 0;
				while ((count = is.read(buffer)) > 0)
				{
					fos.write(buffer, 0, count);
				}

				fos.close();
				is.close();
			}
			SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
					databaseFilename, null);
			return database;
		}
		catch (Exception e)
		{
			Log.d("error", e.getMessage());
		}
		return null;
	}

	@Override
	public boolean onCreate()
	{
		database = openDatabase();
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder)
	{
		Cursor cursor = null;

		switch (uriMatcher.match(uri))
		{
			case CITIES:

				cursor = database.query("v_cities_province", projection,
						selection, selectionArgs, null, null, sortOrder);
				break;
			case CITY_CODE:
				String cityCode = uri.getPathSegments().get(1);
				if (selection == null)
					selection = "city_code='" + cityCode + "'";
				else
					selection += " and (city_code='" + cityCode + "')";
				cursor = database.query("t_cities", projection, selection,
						selectionArgs, null, null, sortOrder);

				break;
			case CITY_NAME:
				String cityName = uri.getPathSegments().get(1);
				if (selection == null)
					selection = "city_name='" + cityName + "'";
				else
					selection += " and (city_name='" + cityName + "')";
				cursor = database.query("t_cities", projection, selection,
						selectionArgs, null, null, sortOrder);

				break;
			case CITIES_IN_PROVINCE:
				String provinceName = uri.getPathSegments().get(1);
				if (selection == null)
					selection = "province_name='" + provinceName + "'";
				else
					selection += " and (province_name='" + provinceName + "')";
				cursor = database.query("v_cities_province", projection, selection,
						selectionArgs, null, null, sortOrder);				
				break;
			
			default:
				throw new IllegalArgumentException("<" + uri + ">格式不正确.");
		}
		return cursor;

	}

	@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 delete(Uri uri, String selection, String[] selectionArgs)
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs)
	{
		// TODO Auto-generated method stub
		return 0;
	}

}
注册provider:

	<provider android:name="RegionContentProvider"
			android:authorities="mobile.android.regioncontentprovider" />
好的,主要代码就在这里,下面我将用过自定义的contentProvider来访问数据库。

package mobile.android.invoke.content.provider;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

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

	public void onClick_Show_Cities(View view)
	{
		ContentResolver contentResolver = getContentResolver();
		Uri uri = Uri
				.parse("content://mobile.android.regioncontentprovider/cities");
		//由于CursorAdapter对象需要_id字段,将city_code的别名设为_id
		Cursor cursor = contentResolver.query(uri, new String[]
		{ "city_code as _id", "city_name", "province_code" }, null, null, null);

		SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_1, cursor, new String[]
				{ "city_name" }, new int[]
				{ android.R.id.text1 });

		ListView lvCities = (ListView) findViewById(R.id.lvCities);
		lvCities.setAdapter(simpleCursorAdapter);

		uri = Uri
				.parse("content://mobile.android.regioncontentprovider/code/024");
		cursor = contentResolver.query(uri, null, null, null, null);
		if (cursor.moveToFirst())
		{
			Toast.makeText(
					this,
					"024:"
							+ cursor.getString(cursor
									.getColumnIndex("city_name")),
					Toast.LENGTH_LONG).show();
		}

		uri = Uri
				.parse("content://mobile.android.regioncontentprovider/name/沈阳");
		cursor = contentResolver.query(uri, null, null, null, null);
		if (cursor.moveToFirst())
		{
			Toast.makeText(
					this,
					"沈阳:"
							+ cursor.getString(cursor
									.getColumnIndex("city_code")),
					Toast.LENGTH_LONG).show();
		}
	}

	public void onClick_Show_Lining_Cities(View view)
	{
		ContentResolver contentResolver = getContentResolver();
		Uri uri = Uri
				.parse("content://mobile.android.regioncontentprovider/cities_in_province/辽宁");
		Cursor cursor = contentResolver.query(uri, new String[]
		{ "city_code as _id", "city_name", "province_code" }, null, null,
				"city_code");

		SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_1, cursor, new String[]
				{ "city_name" }, new int[]
				{ android.R.id.text1 });

		ListView lvCities = (ListView) findViewById(R.id.lvCities);
		lvCities.setAdapter(simpleCursorAdapter);
	}
}


你可能感兴趣的:(内容提供者—Content Provider(二))