使用ContentProvider

一. 了解 ContentProvider 

1.   什么是ContentProvider 

    让开发者在多个应用中操作数据,如存储,修改删除 的唯一方式 ,一个ContentProvider 实现 了下面的接口。

1
2
3
4
5
ContentProvider.insert( Uri ,ContentValues )
ContentProvider.query (Uri ,String [] ,String ,String [], String )
ContentProvider.update(Uri .ConentValues .String ,String []);
ContentProvider.delete( Uri ,String .String );
ContentProvider.getType (Uri );

通过 这些接口,我们不用关心数据 的结构 。

2.   什么是URI

 UUniversal Resources Identifier,在安卓 中,URI有三个部分

(1)  "content://"  ,开头

(2)  数据路径

(3)  ID ,可选 ,如果 不写,所有 的数据 。

content://contacts/peopel

很多常用 的URI安卓 已经定义也常量 。

3.ContentResolver 

      ContentProvider将数据暴露给外面,然后我们用ContentResolve得到数据 。相当 于是一个数据 的消费者,我们用  getContentResolver来得到当前 应用的ContentResolver对象 。

    与ContentProvider一一对应,它有五个接口。

它们将以Cursor的形式返回结果 ,与数据 库相同。


二.  使用ContentProvider

    系统的一些 程序 ,如联系人,通话记录等,往往作为 ContentProvider向外提供 数据 ,我们可以用managedQuery()方法很方便查询相关数据 

1.联系人

三个步骤,我们在这里将Activity   extends ListActivity,重点突出ContentProvider的作用。

(1) 查询联系人,得到Cursor对象 

    managedQuery( Uri uri ,String [] projection ,String selection ,String [] selectionArgs ,String sortOrder)

projection: 要查询的数据 的属性。

(2)新建 一个Adapter

ListAdapter adapter = new SimpleCursorAdapter ( Context context ,int layout ,Cursor c ,String [] from ,int [] to );

(3) 设置Adapter 

  setListAdapter (adapter );

例子:

1
2
3
4
5
6
7
8
9
10
Cursor c = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                 null , null , null , null );
         
         ListAdapter adapter =  new  SimpleCursorAdapter (getApplicationContext(), 
                 android.R.layout.simple_list_item_2, c, 
                 new  String [] {ContactsContract.Contacts.DISPLAY_NAME,
             ContactsContract.CommonDataKinds.Phone.NUMBER}, 
                 new  int  [] {android.R.id.text1,android.R.id.text2});
         
         setListAdapter (adapter);

在ContactsContract中,我们可以找到所有Contacts的信息。

最后要注意,读取联系人时,要的权限 。

1
2
< uses-permission 
     android:name = "android.permission.READ_CONTACTS" ></ uses-permission >

2. 通话记录

和上面的相比 ,要改的是

Uri :CallLog.Calls.CONTENT_URI

还有两个属性: 号码,通话时间

如下:

?

1
2
3
4
5
6
7
8
9
Cursor c = managedQuery(CallLog.Calls.CONTENT_URI,
                 null , null , null , null );
         
         ListAdapter adapter =  new  SimpleCursorAdapter (getApplicationContext(), 
                 android.R.layout.simple_list_item_2, c, 
                 new  String [] {Calls.NUMBER,Calls.DURATION}, 
                 new  int  [] {android.R.id.text1,android.R.id.text2});
         
         setListAdapter (adapter);

3. 多媒体信息

4. 书签


三 .使用ContentResolver 

    外 部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver 类来完成,要获取ContentResolver 对象,可以使用Activity提供的getContentResolver()方法。 ContentResolver使用insert、delete、update、query方法,来操作数据。

1. 删除 数据 

首先,我们要得到ContentResolver 对象 

ContentResolver resolver = getContentResolver();

使用delete

1
ContentResolver.delete (Uri  uri   ,String where ,String [] selectionArgs );

如要删除 所有 的联系人:

resolver.delete( Data.CONTENT_URI  ,null ,null );

如果要删除 名字为WES的,如下 

resolver.delete (Data.CONTENT_URI ,StructuredName.DISPLAY_NAME +"=", new String [] {"WES"});


2. 查询数据 

与数据库类似 

ContentResolver.query (Uri  uri ,String [] projection ,String selection ,String [] SelectionArgs ,String sortOrder );

projection :要查询的属性。

如要查询所有人l,的信息。

1
resolver.query (ContactsContract.CommenDataKinds.Phone,CONTENT_URI ,  null  , null , null null );


3. 更新数据

ContentResolver.update (Uri uri ,ContentValues values ,String where ,String [] selectionArgs );

如下例子:

1
2
values.put(StructruedName.DISPLAY_NAME , "WES"  );
resolver.update(Data.CONTENT_URI , values ,  StruacturedName.DISPLAY_NAME+  "=?" new  String [] { "WES" });

4. 插入数据

这个 看起来很容易 ,但是实际 上很难。

ContentResolver .insert( Uri  uri ,ContentValues values );

暂时不写


你可能感兴趣的:(使用ContentProvider)