安卓 访问内容提供者

访问内容者和提供者相对应  通过数据来获取相应的信息来进行相对的处理

//一个继承andriontextcase的类

package com.xh.tx.test;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;

public class TestPersonContentProvider extends AndroidTestCase
{

	private static final String TAG = "TestPersonContentProvider";

	public void testInsert()
	{
		//访问地址:content://com.xh.tx.contentprivoder.PersonContentPrivoder/person/insert
		
		ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类
		
		ContentValues values = new ContentValues();
		
		values.put("name", "sz");
		values.put("age", "20");
		
		resolver.insert(Uri.parse("content://com.xh.tx.contentprivoder.PersonContentPrivoder/person/insert"), values);
	}
	
	 
	public void testUpdate()
	{
		ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类
		
		ContentValues values = new ContentValues();
		
		values.put("name", "1505");
		String where = "id = ?";
		String[] selectionArgs = new String[]{"1"};
		
		int id = resolver.update(
				Uri.parse("content://com.xh.tx.contentprivoder.PersonContentPrivoder/person/update"), 
				values , 
				where, 
				selectionArgs);
		Log.d(TAG, "UPDATE .... = " + id);
	}
	
	public void testQuery()
	{
		ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类
		
		//content://com.xh.tx.contentprivoder.PersonContentPrivoder/person/query
		String[] projection = new String[]{"id","name","age"};
		
		Cursor cursor = resolver.query(
				Uri.parse("content://com.xh.tx.contentprivoder.PersonContentPrivoder/person/query"), 
				projection, 
				null, 
				null, 
				null);
		
		if(null != cursor && cursor.getCount() > 0)
		{
			while(cursor.moveToNext())
			{
				Integer id = cursor.getInt(0);
				String name = cursor.getString(1);
				Integer age = cursor.getInt(2);
				
				Log.d(TAG, "id=" + id + " name= " + name + " age= " + age);
			}
			
			cursor.close(); // 一定记住不要忘了关闭游标
		}
	}
	
	//短线的内容提供者的访问地址:content//sms/
	
	//周末作业:完成短线的备份和短线的回复
}

你可能感兴趣的:(代码)