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

学习笔记(九)内容提供器

2016-04-29 19:50 1081 查看

一. 简介

  内容提供器 Content Provider 主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性。

  内容提供器可以选择只对哪一部分数据进行共享,从而保证我们程序中的隐私数据不会有泄露的风险。

  内容提供器的用法:

  1. 使用现有的内容提供器来读取和操作相应程序中的数据

  2. 创建自己的内容提供器给我们程序的数据提供外部访问接口

二. 访问其它程序中的数据

  当一个应用程序通过内容提供器对其数据提供了外部访问接口,任何其它的应用程序就都可以对这部分数据进行访问。

1. ContentResolver 的用法

  如果想要访问内容提供器中共享的数据,就一定要借助 ContentResolver 类,可以通过 Context 中的
getContentResolver()
方法获取到该类的实例。

ContentResolver cr = getContentResolver();


  ContentResolver 中的增删改查方法都是不接受表名参数的,而是使用一个 Uri 参数代替,这个参数被称为内容 URI 。

  内容 URI 给内容提供器中的数据建立了唯一表示符,它由权限 authority 和路径 path 组成。权限是用于区分不同的应用程序,用程序包名的方式进行命名;路径是用于区分同一应用程序种不同的表。

  内容 URI 标准格式:

content://com.example.app.provider/table1
content://com.example.app.provider/table2


  得到内容 URI 字符串之后,我们还需要将它解析成 Uri 对象才可以作为参数传入,此时只需要调用
Uri.parse()
方法:

Uri uri = Uri.parse("content://com.example.app.provider/table1");


  现在就可以使用这个 Uri 对象来查询 table1 表中的数据了:

ContentResolver cr = getContentResolver();
//调用query()方法查询数据
Cursor cursor=cr.query(uri, column1, "column1=? and column2=?", new String[] {" "," "});//分别填入uri,查询的列名,查询的约束条件,占位符的值,最后还可以加上排序方式。


  查询完成返回一个 Cursor 对象,这时我们就可以将数据从 Cursor 对象中逐个读取数来:

if(cursor != null){
while(cursor.moveToNext()){
String column1 = cursor.getString(cursor.getColumnIndex("column1"));
int column2 = cursor.getInt(cursor.getColumnIndex("column2"));
}//通过移动游标的位置来遍历Cursor的所有行,然后再取出每一行中相应列的数据
cursor.close();//记得关闭
}


  

  

  添加数据:

ContentValues values = new ContentValues();
values.put("column1","text");//将数据组装到ContentValues中
values.put("column2","1");//
cr.insert(uri, values);//再调用insert()方法


  

  

  更新数据:

ContentValues values = new ContentValues();
values.put("column1","2");//把column1的值改为2
cr.update(uri, values, "column1=? and column2=?", new String[] {"text","1"});//调用update()方法


  

  

  删除数据:

getContentResolver().delete(uri,"column2=?",new String[]{"1"});//删除column2中值为1的行


2. 读取联系人

手机联系人 URI 为:

ContactsContract.CommonDataKinds.Phone.CONTENT_URI

该数据中包括联系人姓名、号码,所在列名分别为:

ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME

ContactsContract.CommonDataKinds.Phone.NUMBER

读取联系人信息,还需要获取权限:

android.permission.READ_CONTACTS

public class MainActivity extends AppCompatActivity {

ListView contactsView;
ArrayAdapter<String> adapter;
List<String> contactsList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactsView = (ListView) findViewById(R.id.contacts_view);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contactsList);
contactsView.setAdapter(adapter);
readContacts();
}

private void readContacts(){
ContentResolver cr = getContentResolver();
Cursor cursor = null;
try{
//查询联系人数据
cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
while(cursor.moveToNext()){
//获取联系人姓名
String displayName = cursor.getString(cursor.getColumnIndex(Contac
4000
tsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//获取联系人手机号
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactsList.add(displayName + "\n" + number);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(cursor != null){
cursor.close();//记得关闭Cursor对象
}
}
}


三. 创建自己的内容提供器

1. URI 介绍

URI:URI 的目的是为了能够根据 URI 以及调用的方法来决定怎样操作数据。

URI 包括两种格式:

content://com.example.app.provider/table1
content://com.example.app.provider/table1/1


第一种格式,以路径 table1 结尾,访问表 table1 中所有数据;

第二种格式,在最后加上了 id,即访问表 table1 中 id 为 1 的数据。

或以通配符形式表示:

*:代表任意长度任意字符;

#:代表任意长度任意数字。

content://com.example.app.provider/*    //代表任意一个表
content://com.example.app.provider/table1/# //代表table1中任意一行


2. ContentProvider 开发步骤简要说明

(1) 首先,新建 MyContentProvider 类去继承 ContentProvider,需要重写 6 个方法:

onCreate()
:当存在 ContentResolver 尝试访问该 CP 时,自动调用
onCreate()
方法来初始化 CP,通常在此完成数据库创建、升级等操作。

insert()
query()
update()
delete()
就是操作 CP 提供的数据时使用的方法。

getType()
:根据传入的 URI 来返回相应的 MIME 类型。

(2)在 AndroidManifest.xml 中注册

<provider
android:name="com.example.contentprovider.MyContentProvider"
android:authorities="com.example.contentprovider.provider"
android:exported="true" >
</provider>


android:exported="true"
表示该 provider 可以被其他程序访问。

(3) 定义 UriMatcher

借助 UriMatcher 这个类实现匹配内容 URI 的功能:

首先,配置 UriMatcher,利用
addURI()
方法,将权限,路径和自定义代码传入;

然后,解析 URI,利用
match(uri)
方法,如果该 uri 和之前
addURI()
写入的权限和路径相同时,返回自定义代码;

最后,通过这个自定义代码,即可采取不同的操作。

public class MyContentProvider extends ContentProvider{
public static final int TABLE1_DIR=0;
public static final int TABLE1_ITEM=1;
public static final int TABLE2_DIR=2;
public static final int TABLE2_ITEM=3;
private static UriMatcher uriMatcher;
static{
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.example.contentprovider", "table1", TABLE1_DIR);
uriMatcher.addURI("com.example.contentprovider", "table1/#", TABLE1_ITEM);
uriMatcher.addURI("com.example.contentprovider", "table2", TABLE2_DIR);
uriMatcher.addURI("com.example.contentprovider", "table2/#", TABLE2_ITEM);
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {
switch (uriMatcher.match(uri)) {
case TABLE1_DIR:
//查询table1中所有数据
break;
case TABLE1_ITEM:
//查询table1中单条数据
break;
case TABLE2_DIR:
//查询table2中单条数据
break;
case TABLE2_ITEM:
//查询table2中单条数据
break;
default:
break;
}
...
}
...
}


(4)重写方法

onCreate()
:用于为操作数据做准备;

insert()
:插入数据,返回插入的记录所代表的URI;

update()
:更新数据,返回操作影响的记录行数;

delete()
:删除数据,返回操作影响的记录行数;

query()
:查询数据,返回Cursor;

getType()
:记录的类型,所有内容提供器都必须提供的一个方法,用于获取 URI 对象所对应的 MIME 类型。

一个内容 URI 对应的 MIME 字符串包含 3 个部分:

1. 必须以 vnd 开头;

2. 如果内容 URI 以路径结尾,则后接 android.cursor.dir/

如果内容 URI 以 id 结尾,则后接 android.cursor.item/

3. 最后接上
vnd.<authority>.<path>


例如:

URI: content://com.example.app.provider/table1

对应的MIME: vnd.android.cursor.dir/vnd.com.example.app.provider.table1

URI: content://com.example.app.provider/table1/1

对应的MIME: vnd.android.cursor.item/vnd.com.example.app.provider.table1

(5)外部调用

ContentResolver resolver = this.getContext().getContentResolver();

resolver.insert();

resolver.update();

resolver.delete();

resolver.query();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android 学习笔记