Read system contacts
Since we have been using simulators for a long time, there are no contacts in the phone book, so now you need to add them manually so you can read them later. Open the phonebook program, as shown in interface 7.1.
Figure 7.1
As you can see, there are no contacts in the phone book, and we can create them by clicking the Create a new contact button. First, create two contacts, fill in their name and mobile number, as shown in Figure 7.2.
Figure 7.2
So the preparation is done, now create a new Contactstest project, let's get started. The first thing to do is to write the layout file, where we want the contact information to be read in the ListView
Display, therefore, modify the code in the Activity_main.xml as follows:
<linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" >
<listviewandroid:id= "@+id/contacts_view" android:layout_width= "match_parent" android:layout_height= "Match_ Parent ">
</ListView>
</LinearLayout>
For simplicity's sake, only one ListView is placed in the linearlayout. Then modify the code in the Mainactivity,
As shown below:
public class Mainactivityextends Activity {ListView contactsview; Arrayadapter<string> adapter;
list<string> contactslist = new arraylist<string> ();
@Override
Protected voidoncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);
Contactsview = (ListView) Findviewbyid (R.id.contacts_view);
adapter = newarrayadapter<string> (this, Android. R.layout. simple_list_item_1,contactslist);
Contactsview.setadapter (adapter);
Readcontacts ();
}
Private voidreadcontacts () {cursor cursor = NULL;
try {
// query contact data
Cursor =getcontentresolver (). query (contactscontract.commondatakinds.phone.content_uri,null, NULL, NULL, NULL);
while (Cursor.movetonext ()) {
// Get contact Name
String displayName = cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)) ;
// Get contact phone number
String number =cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER));
Contactslist.add (displayName + "\ n" + number);
}
} catch (Exception e) {
E.printstacktrace ();
} finally {
if (cursor! = NULL) {
Cursor.close ();
}
}
}
}
In the OnCreate () method, we first get an instance of the ListView control, set it up with an adapter, and then call the Readcontacts () method. Here's a look at the Readcontacts () method, where you can see that the Contentresolver query () method is used for querying the system's contact data. But what's so strange about the incoming URI parameter, why didn't you call the Uri.parse () method to parse a content URI string? This is because the ContactsContract.CommonDataKinds.Phone class has already done the encapsulation for us, providing a Content_uri constant, which is the result of parsing it using the Uri.parse () method. We then iterate over the Cursor object, taking the contact name and phone number out of the data one by one, and the name of the contact is the constant of the column ContactsContract.CommonDataKinds.Pho Ne. Display_name, contact phone number the corresponding constant for this column is ContactsContract.CommonDataKinds.Phone.NUMBER. After all two data have been removed, they are stitched together with a newline character in the middle, and then the stitched data is added to the ListView. Finally, don't forget to close the Cursor object.
Is this the end of it? A little bit worse, reading system contacts also requires declaring permissions, so modify the code in Androidmanifest.xml as follows:
<manifestxmlns:android= "Http://schemas.android.com/apk/res/android" package= "Com.example.contactstest"
Android:versioncode= "1"
Android:versionname= "1.0" >
......
<uses-permissionandroid:name= "Android.permission.READ_CONTACTS"/>
......
</manifest>
Added the Android.permission.READ_CONTACTS permission so that our program can access the system's contact data. Now it's done, let's run the program, as shown in effect 7.3.
Figure 7.3
The data for the two contacts you just added have been successfully read! It is true that the ability to access data across programs is implemented.
Android: Read system contacts via content provider