Android Application Development Notes (2): Read the contact information in the mobile phone (android. provider. ContactsContract)

Source: Internet
Author: User

This article from http://blog.csdn.net/xjanker2, reference reprint must indicate the source!

In the previous article, we used the call and text message function, but we do not know the contact information. This article describes how to get the contact information of the Local Machine from Android.

Due to the rapid Android version upgrade, Some APIs have changed. This article describes how to use the ContentProvider mechanism to retrieve Contacts. The original android. provider. Contacts class and its related classes are replaced by android. provider. ContactsContract. The original class system is marked as Deprecated because it still exists due to compatibility, but it cannot be completely discarded in later versions.

 

Therefore, this article begins with reading from contacts on platforms above Android 2.2. the following code runs in Android,

 

 

First, create ViewContacts class to inherit the ListActivity and set it to the start Activity of the application.

ViewContacts. java code:

 Package jtapp. contacts; </p> <p> import java. util. arrayList; <br/> import java. util. hashMap; <br/> import java. util. list; </p> <p> import android. app. listActivity; <br/> import android. database. cursor; <br/> import android. OS. bundle; <br/> import android. provider. contactsContract; <br/> import android. widget. simpleAdapter; </p> <p> public class ViewContacts extends ListActivity {<br/>/** Called when the activit Y is first created. */<br/> @ Override <br/> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); <br/> setContentView (R. layout. main); </p> <p> List <HashMap <String, String> items = fillMaps (); <br/> SimpleAdapter adapter = new SimpleAdapter (<br/> this, items, R. layout. list_item, <br/> new String [] {"name", "key"}, <br/> new int [] {R. id. item, R. id. item2}); <br/> thi S. setListAdapter (adapter); </p> <p >}</p> <p> private List <HashMap <String, String> fillMaps () {<br/> List <HashMap <String, String> items = new ArrayList <HashMap <String, String> (); </p> <p> Cursor cur = null; <br/> try {<br/> // Query using ContentResolver. query or Activity. managedQuery <br/> cur = getContentResolver (). query (<br/> ContactsContract. contacts. CONTENT_URI, null, null); <br/> if (cu R. moveToFirst () {<br/> int idColumn = cur. getColumnIndex (<br/> ContactsContract. contacts. _ ID); <br/> int displayNameColumn = cur. getColumnIndex (<br/> ContactsContract. contacts. DISPLAY_NAME); <br/> // Iterate all users <br/> do {<br/> String contactId; <br/> String displayName; <br/> String phoneNumber = ""; <br/> // Get the field values <br/> contactId = cur. getString (idColumn); <br/> displayName = cu R. getString (displayNameColumn); <br/> // Get number of user's phoneNumbers <br/> int numberCount = cur. getInt (cur. getColumnIndex (<br/> ContactsContract. contacts. HAS_PHONE_NUMBER); <br/> if (numberCount> 0) {<br/> Cursor phones = getContentResolver (). query (<br/> ContactsContract. commonDataKinds. phone. CONTENT_URI, <br/> null, <br/> ContactsContract. commonDataKinds. phone. CONTACT_ID <br/> + "=" + contactI D <br/>/* + "and" + ContactsContract. commonDataKinds. phone. TYPE <br/> + "=" + ContactsContract. commonDataKinds. phone. TYPE_MOBILE */, <br/> null, null); <br/> if (phones. moveToFirst () {<br/> int numberColumn = phones. getColumnIndex (<br/> ContactsContract. commonDataKinds. phone. NUMBER); <br/> // Iterate all numbers <br/> do {<br/> phoneNumber + = phones. getString (numberColumn) + ","; <br/>}while (phones. MoveToNext (); <br/>}< br/> // Add values to items <br/> HashMap <String, string> I = new HashMap <String, String> (); <br/> I. put ("name", displayName); <br/> I. put ("key", phoneNumber); <br/> items. add (I); <br/>}while (cur. moveToNext (); <br/>} else {<br/> HashMap <String, String> I = new HashMap <String, String> (); <br/> I. put ("name", "Your Phone"); <br/> I. put ("key", "Have No Contacts. "); <br/> items. Add (I); <br/>}< br/>}finally {<br/> if (cur! = Null) <br/> cur. close (); <br/>}< br/> return items; <br/>}< br/>}

 

Main. xml code:

<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: orientation = "vertical" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "fill_parent" <br/> <TextView <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" <br/> android: text = "show your phone's contacts: "<br/> <ListView android: id =" @ id/android: list "<br/> android: layout_width = "fill_parent" <br/> android: layout_height = "0dip" <br/> android: layout_weight = "1" <br/> android: drawselectofftop = "false" <br/> </LinearLayout>

List_item.xml code:

<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: orientation = "vertical" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "fill_parent" <br/> <TableRow android: id = "@ + id/TableRow01" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content"> <br/> <TextView android: id = "@ + id/item" <br/> xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> android: textSize = "20px"/> <br/> <TextView android: text = ":" <br/> xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> android: textSize = "20px"/> <br/> <TextView android: id = "@ + id/item2" <br/> xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> android: textSize = "20px"/> <br/> </TableRow> <br/> </LinearLayout>

 

Added the uses permission READ_CONTACTS code for AndroidManifest. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "jtapp. contacts "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <br/> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". viewcontacts "Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-Permission Android: name = "android. permission. read_contacts "> </uses-Permission> <br/> </manifest>

 

After the above files are compiled, the application can run correctly on the Android2.1 simulator.

 

What if the app runs on android1.6? 1.6 does not have the ContactsContract class system, so an error will be reported. Note that the ContactContract class is added in API Level 5, which is not supported in earlier Android versions.

On Android 1.6 (API Level 4), The fillMaps () method is implemented as follows:

 Private List <HashMap <String, String> fillMaps () {<br/> List <HashMap <String, String> items = new ArrayList <HashMap <String, string >>(); </p> <p> Cursor cur = null; <br/> try {<br/> // Form an array specifying which columns to return. <br/> String [] projection = new String [] {<br/> People. _ ID, <br/> People. NAME, <br/> People. NUMBER <br/>}; <br/> // query using ContentResolver. query or Activity. managedQuery <Br/> cur = getContentResolver (). query (<br/> People. CONTENT_URI, projection, null); <br/> if (cur. moveToFirst () {<br/> String name; <br/> String phoneNumber; <br/> int nameColumn = cur. getColumnIndex (People. NAME); <br/> int phoneColumn = cur. getColumnIndex (People. NUMBER); <br/> do {<br/> // Get the field values <br/> name = cur. getString (nameColumn); <br/> phoneNumber = cur. getString (phoneColum N); <br/> // Do something with the values. <br/> HashMap <String, String> I = new HashMap <String, String> (); <br/> I. put ("name", name); <br/> I. put ("key", phoneNumber); <br/> items. add (I); <br/>}while (cur. moveToNext (); <br/>} else {<br/> HashMap <String, String> I = new HashMap <String, String> (); <br/> I. put ("name", "Your Phone"); <br/> I. put ("key", "Have No Contacts. "); <br/> items. add (I); <br/>}< br/>} Finally {<br/> if (cur! = Null) <br/> cur. close (); <br/>}< br/> return items; <br/>}

 

Then it can run on 1.6. The effect is as follows:

The contact API is changed after Android 1.6. If you call the API in the above version 2.1, you will find that the name is available under Android, but the phone number is not displayed. After careful observation, we can find that calls such as People. CONTENT_URI are marked in more than 2.0 of the sdks.Deprecated. This makes it difficult to compile applications that wish to be compatible with both version 1.6 and version 2. x. If the application involves reading contacts, do I have to write multiple versions of apk? In fact, we can use the method of judging the current system API Level to write two sets of code for backup, which is left for everyone to practice.

 

Method for obtaining the system API level:

Int version = android. provider. settings. system. getint (context <br/>. getcontentresolver (), <br/> android. provider. settings. system. sys_prop_setting_version, <br/> 3); </P> <p>

Related Article

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.