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