Address book example implemented by calling system URI through contentresolver

Source: Internet
Author: User

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

  1. ImportAndroid. content. contentresolver;
  2. ImportAndroid. content. contenturis;
  3. ImportAndroid. content. contentvalues;
  4. ImportAndroid. content. context;
  5. ImportAndroid. provider. contactscontract. Data;
  6. ImportAndroid. provider. contactscontract. rawcontacts;
  7. ImportAndroid. provider. contactscontract. commondatakinds. Phone;
  8. ImportAndroid. provider. contactscontract. commondatakinds. structuredname;
  9. Public ClassContacthandler {
  10. PrivateContext context;
  11. PublicContacthandler (context ){
  12. This. Context = context;
  13. }
  14. Public VoidCreate (contactitem item ){
  15. Contentresolver resolver = context. getcontentresolver ();
  16. Contentvalues values =NewContentvalues ();
  17. LongRawcontactid = contenturis. parseid (resolver. insert (rawcontacts. content_uri, values ));
  18. Values. Clear ();
  19. Values. Put (data. raw_contact_id, rawcontactid );
  20. Values. Put (data. mimetype, structuredname. content_item_type );
  21. Values. Put (structuredname. display_name, item. getvalue (contactitem. type_name ));
  22. Resolver. insert (data. content_uri, values );
  23. Values. Clear ();
  24. Values. Put (data. raw_contact_id, rawcontactid );
  25. Values. Put (data. mimetype, phone. content_item_type );
  26. Values. Put (phone. Number, item. getvalue (contactitem. type_phone ));
  27. Values. Put (phone. type, phone. type_mobile );
  28. Resolver. insert (data. content_uri, values );
  29. }
  30. Public VoidDelete (contactitem item ){
  31. Contentresolver resolver = context. getcontentresolver ();
  32. String [] ARGs = {item. getvalue (contactitem. type_contact_id )};
  33. Resolver. Delete (rawcontacts. content_uri, Data. contact_id +"=? ", ArgS );
  34. Resolver. Delete (data. content_uri, Data. raw_contact_id +"=? ", ArgS );
  35. }
  36. }

 

The following is a class contractgenerator that obtains the address book list information at a time:

 

[Java] View plaincopy

  1. ImportJava. Io. inputstream;
  2. ImportJava. util. arraylist;
  3. ImportJava. util. List;
  4. ImportAndroid. content. contentresolver;
  5. ImportAndroid. content. contenturis;
  6. ImportAndroid. content. context;
  7. ImportAndroid. database. cursor;
  8. ImportAndroid. Graphics. Bitmap;
  9. ImportAndroid. Graphics. bitmapfactory;
  10. ImportAndroid.net. Uri;
  11. ImportAndroid. provider. contactscontract. contacts;
  12. ImportAndroid. provider. contactscontract. commondatakinds. Phone;
  13. ImportAndroid. provider. contactscontract. commondatakinds. Photo;
  14. Public ClassContactgenerator {
  15. PrivateContext context;
  16. Private Static FinalString [] projectection = {
  17. Phone. display_name,
  18. Phone. Number,
  19. Photo. photo_id,
  20. Phone. contact_id,
  21. };
  22. Private Static Final IntDisplay_name_index =0;
  23. Private Static Final IntPhone_number_index =1;
  24. Private Static Final IntPhoto_id_index =2;
  25. Private Static Final IntContact_id_index =3;
  26. PublicContactgenerator (context ){
  27. This. Context = context;
  28. }
  29. PublicList <contactitem> generatelist (){
  30. List <contactitem> List =NewArraylist <contactitem> ();
  31. Contentresolver resolver = context. getcontentresolver ();
  32. Cursor cursor = resolver. Query (phone. content_uri, projectection,Null,Null, Projectection [display_name_index] +"ASC");
  33. While(Cursor. movetonext ()){
  34. String name = cursor. getstring (display_name_index );
  35. String phone = cursor. getstring (phone_number_index );
  36. LongPhotoid = cursor. getlong (photo_id_index );
  37. LongContactid = cursor. getlong (contact_id_index );
  38. Bitmap head =Null;
  39. If(Photoid>0){
  40. Uri uri = contenturis. withappendedid (contacts. content_uri, contactid );
  41. Inputstream input = contacts. opencontactphotoinputstream (Resolver, Uri );
  42. Head = bitmapfactory. decodestream (input );
  43. }
  44. Contactitem item =NewContactitem (context, name, phone, head );
  45. Item. setcontactid (contactid );
  46. List. Add (item );
  47. }
  48. ReturnList;
  49. }
  50. }

 

To operate the system Address Book database, you must add the following two user permissions to the androidmanifest. xml file:

 

[HTML] View plaincopy

    1. <Uses-Permission Android: Name="Android. Permission. read_contacts"></Uses-Permission>
    2. <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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.