Android Read contact information

Source: Internet
Author: User

The contact information is stored in a contacts2.db database.

The two main tables

Layout file

Define a button in the layout file to get the event that triggered the get contact information

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ButtonAndroid:text= "Get contact information"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"/></LinearLayout>

Activity

In activity we create a content visitor to access the data under the Contact app (which is provided by the content provider for the contact app). The content provider provides an interface to local database access, where the contact information is stored by multiple tables, and is used primarily in two tables. So at least two queries are made to the database.

 PackageXidian.dy.com.chujia;ImportAndroid.content.ContentResolver;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View; Public classMainactivityextendsappcompatactivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.main); }     Public voidClick (View v) {contentresolver cr=Getcontentresolver (); Cursor Cursorid= Cr.query (Uri.parse ("Content://com.android.contacts/raw_contacts"),Newstring[]{"contact_id"},NULL,NULL,NULL); if(Cursorid! =NULL){             while(Cursorid.movetonext ()) {String ID= cursorid.getstring (0); Cursor Contact= Cr.query (Uri.parse ("Content://com.android.contacts/data"),Newstring[]{"Data1", "MimeType"},                        "Raw_contact_id=?",NewString[]{id},NULL); if(Contact! =NULL){                     while(Contact.movetonext ()) {String name= contact.getstring (0);                    SYSTEM.OUT.PRINTLN (name);                } contact.close ();        }} cursorid.close (); }    }}

Check the contact ID first, and then the contact's ID to see the contact details.

Manifest file

Permission to access contacts needs to be turned on in the manifest file.

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Xidian.dy.com.chujia">    <uses-permissionAndroid:name= "Android.permission.READ_CONTACTS"/><ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme">    <ActivityAndroid:name=". Mainactivity "Android:label= "Main interface">        <Intent-filter>            <ActionAndroid:name= "Android.intent.action.MAIN" />            <categoryAndroid:name= "Android.intent.category.LAUNCHER" />        </Intent-filter>    </Activity></Application></Manifest>

Android Read contact information

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.