您的位置:首页 > 移动开发 > Android开发

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

2015-10-27 22:46 846 查看

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: