A program for reading contact information in Android, including reading the contact name, mobile phone number, and email address

Source: Internet
Author: User

1: androidmanifest. XML content

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cn.itcast.contacts"      android:versionCode="1"      android:versionName="1.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">     <uses-library android:name="android.test.runner" />        <activity android:name=".MainActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>    <uses-sdk android:minSdkVersion="8" />    <uses-permission android:name="android.permission.READ_CONTACTS" />    <uses-permission android:name="android.permission.WRITE_CONTACTS" />         <instrumentation android:name="android.test.InstrumentationTestRunner"     android:targetPackage="cn.itcast.contacts" android:label="Tests for My App" /></manifest> 

It is important to set up a test environment and add the <uses-Permission> permission to read and write contact information.

2: main method of reading

/** Read the contact information */Public void testreadallcontacts () {cursor = This. getcontext (). getcontentresolver (). query (contactscontract. contacts. content_uri, null, null); int contactidindex = 0; int nameindex = 0; If (cursor. getcount ()> 0) {contactidindex = cursor. getcolumnindex (contactscontract. contacts. _ id); nameindex = cursor. getcolumnindex (contactscontract. contacts. display_name);} while (cursor. movetonext () {string contactid = cursor. getstring (contactidindex); string name = cursor. getstring (nameindex); log. I (TAG, contactid); log. I (TAG, name);/** find the phone information of the contact */cursor phones = This. getcontext (). getcontentresolver (). query (contactscontract. commondatakinds. phone. content_uri, null, contactscontract. commondatakinds. phone. contact_id + "=" + contactid, null, null); int phoneindex = 0; If (phones. getcount ()> 0) {phoneindex = phones. getcolumnindex (contactscontract. commondatakinds. phone. number);} while (phones. movetonext () {string phonenumber = phones. getstring (phoneindex); log. I (TAG, phonenumber);}/** find the contact's email information */cursor emails = This. getcontext (). getcontentresolver (). query (contactscontract. commondatakinds. email. content_uri, null, contactscontract. commondatakinds. email. contact_id + "=" + contactid, null, null); int emailindex = 0; If (emails. getcount ()> 0) {emailindex = emails. getcolumnindex (contactscontract. commondatakinds. email. data);} while (emails. movetonext () {string email = emails. getstring (emailindex); log. I (TAG, email );}}}

 

3: Currently, there are two contacts in the mobile phone ,,

This is the contact information of hellen.

This is the contact information of Mike.

 

4: test result. The output content on the console is:

You can see that two contacts are output in the console.

 

//--------------------------------------

Get all group numbers and group names

Cursor groupInfoCursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, null, null, null);while(groupInfoCursor.moveToNext()) {    int idIndex = groupInfoCursor.getColumnIndex(ContactsContract.Groups._ID);    int titleIndex = groupInfoCursor.getColumnIndex(ContactsContract.Groups.TITLE);    String id = groupInfoCursor.getString(idIndex);    String title = groupInfoCursor.getString(titleIndex);}groupInfoCursor.close();

 

 

 

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.