Android四大组件之Content Provider(内容提供者)

本文详细介绍了Android中的ContentProvider组件,包括其概念、应用场景及具体实现步骤,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Content Provider简介

1、什么是Content Provider

   所谓内容提供者官方给出的概念是:为存储和获取数据提供统一的接口。可以在不同的应用程序之间共享数据。

2、什么时候使用


   一般情况下我们知道自己应用程序的数据库是私有的,别的应用是不能进行访问的,可是有时我们需要把应用的数据库暴露给其他的应用程序进行增删改查,比如一些备份手机联系人的应用,这时就可以用到了Content Provider。

3、怎么用


(1)编写一个类继承ContentProvider,实现里面增删改查的方法

(2)在清单文件中配置内容提供者,注意指定android:authorities="com.xxx.db"(这个是口令,如果那个应用想用ContentProvider需要匹配口令,为了可读性强,一般用所在项目的包名来命名)

(3)在内容提供者的类的内部声明UriMatcher,用来为别的应用匹配口令

(4)为mUriMatcher添加需要检测的uri,调用mUriMatcher.addURI("com.itheima.db", "account", SUCCESS);(uri就是上面"com.xxx.db",第二个参数是为了精确匹配又加的一个“暗语”,第三个参数是判断匹配成功的标示)。

(5)调用mUriMatcher.match(uri)来检查uri路径是否正确

(6)在另外一个应用程序里面 通过contentResolver 增删改查

4、示例代码


内容提供者代码:

package com.itheima.db;

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;

/**
 * 内容提供者 后门程序,提供私有的数据给别的应用程序,默认都是空实现.
 */
public class BankDBBackdoor extends ContentProvider {

	public static final int SUCCESS = 1;
	/**
	 * 创建一个保安,检查uri的规则,如果uri匹配失败 返回-1
	 */
	static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	static {
		mUriMatcher.addURI("com.itheima.db", "account", SUCCESS);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int code = mUriMatcher.match(uri);
		if (code == SUCCESS) {
			System.out.println("delete 删除数据");
			MyDBOpenHelper helper = new MyDBOpenHelper(getContext());
			SQLiteDatabase db = helper.getWritableDatabase();
			db.delete("account",selection , selectionArgs);
			//利用内容提供者的解析器,通知内容观察者数据发生了变化
			getContext().getContentResolver().notifyChange(uri, null);
		}else{
			throw new IllegalArgumentException("口令 不正确,滚犊子");
		}
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		int code = mUriMatcher.match(uri);
		if (code == SUCCESS) {
			System.out.println("insert 添加数据");
			MyDBOpenHelper helper = new MyDBOpenHelper(getContext());
			SQLiteDatabase db = helper.getWritableDatabase();
			db.insert("account", null, values);
			//利用内容提供者的解析器,通知内容观察者数据发生了变化
			getContext().getContentResolver().notifyChange(uri, null);
		}else{
			throw new IllegalArgumentException("口令 不正确,滚犊子");
		}
		return null;
	}

	@Override
	public boolean onCreate() {
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		int code = mUriMatcher.match(uri);
		if (code == SUCCESS) {
			System.out.println("query 查询数据");
			MyDBOpenHelper helper = new MyDBOpenHelper(getContext());
			SQLiteDatabase db = helper.getReadableDatabase();
			return db.query("account", projection, selection, selectionArgs, null, null, sortOrder);
		}else{
			throw new IllegalArgumentException("口令 不正确,滚犊子");
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int code = mUriMatcher.match(uri);
		if (code == SUCCESS) {
			System.out.println("update 更新数据");
			MyDBOpenHelper helper = new MyDBOpenHelper(getContext());
			SQLiteDatabase db = helper.getWritableDatabase();
			db.update("account", values, selection, selectionArgs);
			//利用内容提供者的解析器,通知内容观察者数据发生了变化
			getContext().getContentResolver().notifyChange(uri, null);
		}else{
			throw new IllegalArgumentException("口令 不正确,滚犊子");
		}
		return 0;
	}

}

调用内容提供者的代码:

package com.itheima.bankboss;

import java.util.Currency;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
/**
 * 使用内容提供者的类
 * @author LBK
 *
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	/**
	 *  利用后门 添加一条数据
	 */
	public void insert(View view) {
		// 得到内容提供者的解析器
		ContentResolver resolver = getContentResolver();
		Uri uri = Uri.parse("content://com.itheima.db/account");
		ContentValues values = new ContentValues();
		values.put("name", "zhangsan");
		values.put("money", 10000);
		// 通过内容解析器让内容提供者添加一条数据
		resolver.insert(uri, values);
	}

	public void delete(View view) {
		ContentResolver resolver = getContentResolver();
		Uri uri = Uri.parse("content://com.itheima.db/account");
		resolver.delete(uri, "name=?", new String[]{"zhangsan"});
	}

	public void update(View view) {
		ContentResolver resolver = getContentResolver();
		Uri uri = Uri.parse("content://com.itheima.db/account");
		ContentValues values = new ContentValues();
		values.put("money", 20000);
		resolver.update(uri, values, "name=?", new String[]{"zhangsan"});
	}

	public void query(View view) {
		ContentResolver resolver = getContentResolver();
		Uri uri = Uri.parse("content://com.itheima.db/account");
		Cursor cursor = resolver.query(uri, new String[]{"name","money"}, null, null, null);
		while(cursor.moveToNext()){
			String name = cursor.getString(0);
			float money = cursor.getFloat(1);
			System.out.println("name:"+name+"----"+"money:"+money);
		}
		cursor.close();
	}
}





评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值