From: http://blog.csdn.net/sadfishsc/article/details/7406327
One of the four components of Android, contentprovider, is divided into two parts: one is the data organization tool contentprovider and the other is the data call tool contentresolver. For the Uris provided by the system, such as Address Book, multimedia, and SMS, you do not need to create additional databases or organize them with contentprovider. Instead, you can directly use contentresolver to call these Uris, adds, deletes, modifies, and queries System databases to ensure data consistency across Android devices.
The following link is the source code of the address book demo I made to participate in a small competition on the MM forum some time ago. Due to some recent work and life reasons, the deadline for the competition was delayed, so no artist was done at the end.
Http://download.csdn.net/detail/sadfishsc/4182452
The Analysis of Address Book URI mainly refers to the blog of Yu Song Momo:
Http://blog.csdn.net/xys289187120/article/details/6730957
The following is the contacthandler class for adding and deleting Address Book tables:
[Java] View plaincopy
-
- ImportAndroid. content. contentresolver;
-
- ImportAndroid. content. contenturis;
- ImportAndroid. content. contentvalues;
-
- ImportAndroid. content. context;
-
- ImportAndroid. provider. contactscontract. Data;
-
- ImportAndroid. provider. contactscontract. rawcontacts;
-
- ImportAndroid. provider. contactscontract. commondatakinds. Phone;
-
- ImportAndroid. provider. contactscontract. commondatakinds. structuredname;
-
- Public ClassContacthandler {
-
- PrivateContext context;
-
-
- PublicContacthandler (context ){
-
- This. Context = context;
-
- }
-
- Public VoidCreate (contactitem item ){
-
- Contentresolver resolver = context. getcontentresolver ();
-
- Contentvalues values =NewContentvalues ();
-
-
- LongRawcontactid = contenturis. parseid (resolver. insert (rawcontacts. content_uri, values ));
-
- Values. Clear ();
-
- Values. Put (data. raw_contact_id, rawcontactid );
-
- Values. Put (data. mimetype, structuredname. content_item_type );
-
- Values. Put (structuredname. display_name, item. getvalue (contactitem. type_name ));
-
- Resolver. insert (data. content_uri, values );
-
- Values. Clear ();
-
- Values. Put (data. raw_contact_id, rawcontactid );
-
- Values. Put (data. mimetype, phone. content_item_type );
-
- Values. Put (phone. Number, item. getvalue (contactitem. type_phone ));
-
- Values. Put (phone. type, phone. type_mobile );
-
- Resolver. insert (data. content_uri, values );
-
- }
-
- Public VoidDelete (contactitem item ){
-
- Contentresolver resolver = context. getcontentresolver ();
-
-
- String [] ARGs = {item. getvalue (contactitem. type_contact_id )};
-
- Resolver. Delete (rawcontacts. content_uri, Data. contact_id +"=? ", ArgS );
-
- Resolver. Delete (data. content_uri, Data. raw_contact_id +"=? ", ArgS );
-
- }
-
- }
The following is a class contractgenerator that obtains the address book list information at a time:
[Java] View plaincopy
-
- ImportJava. Io. inputstream;
-
- ImportJava. util. arraylist;
- ImportJava. util. List;
-
-
- ImportAndroid. content. contentresolver;
-
- ImportAndroid. content. contenturis;
-
- ImportAndroid. content. context;
-
- ImportAndroid. database. cursor;
-
- ImportAndroid. Graphics. Bitmap;
-
- ImportAndroid. Graphics. bitmapfactory;
- ImportAndroid.net. Uri;
-
- ImportAndroid. provider. contactscontract. contacts;
-
- ImportAndroid. provider. contactscontract. commondatakinds. Phone;
-
- ImportAndroid. provider. contactscontract. commondatakinds. Photo;
-
-
- Public ClassContactgenerator {
-
- PrivateContext context;
-
- Private Static FinalString [] projectection = {
-
- Phone. display_name,
-
- Phone. Number,
-
- Photo. photo_id,
-
- Phone. contact_id,
-
- };
- Private Static Final IntDisplay_name_index =0;
-
- Private Static Final IntPhone_number_index =1;
- Private Static Final IntPhoto_id_index =2;
-
- Private Static Final IntContact_id_index =3;
-
-
- PublicContactgenerator (context ){
- This. Context = context;
-
- }
-
-
- PublicList <contactitem> generatelist (){
-
- List <contactitem> List =NewArraylist <contactitem> ();
-
- Contentresolver resolver = context. getcontentresolver ();
-
- Cursor cursor = resolver. Query (phone. content_uri, projectection,Null,Null, Projectection [display_name_index] +"ASC");
-
- While(Cursor. movetonext ()){
-
- String name = cursor. getstring (display_name_index );
- String phone = cursor. getstring (phone_number_index );
-
- LongPhotoid = cursor. getlong (photo_id_index );
-
- LongContactid = cursor. getlong (contact_id_index );
-
-
- Bitmap head =Null;
- If(Photoid>0){
-
- Uri uri = contenturis. withappendedid (contacts. content_uri, contactid );
-
- Inputstream input = contacts. opencontactphotoinputstream (Resolver, Uri );
-
- Head = bitmapfactory. decodestream (input );
- }
-
-
- Contactitem item =NewContactitem (context, name, phone, head );
-
- Item. setcontactid (contactid );
-
- List. Add (item );
-
- }
-
- ReturnList;
-
- }
-
- }
To operate the system Address Book database, you must add the following two user permissions to the androidmanifest. xml file:
[HTML] View plaincopy
- <Uses-Permission Android: Name="Android. Permission. read_contacts"></Uses-Permission>
- <Uses-Permission Android: Name="Android. Permission. write_contacts"></Uses-Permission>
The usage of contentresolver involves the following steps:
1. Get the contentresolver instance through the getcontentresolver () method of context;
2. call the insert, delete, update, and query methods of contentresolver to add, delete, modify, and query data tables. The system URI is used to specify the data tables, these Uris are passed into the preceding four methods as parameters. For more information, see the android API documentation.
In addition, the following blog posts Share System Uris including address book, multimedia, and SMS:
Http://blog.csdn.net/lyx2007825/article/details/7280043
For the example in this article, the Java source code file is as follows:
In addition to the preceding two operations on contentresolver, the UI control of this address book is mainly based on the level-2 list expandablelistview. Therefore, there is a level-2 List Data Processing adapter class contactadapter inherited from baseexpandablelistadapter. In addition, contactitem is a data entity class. The interface class of the other two activity classes.
Finally, it should be noted that there is no need to use contentprovider to encapsulate and manage the URI data resources provided by the system. Therefore, contentprovider is not used in this example and only contentresolver is used.