35. Content provider: Contentresolver
Use content providers to get contact information
35-1: Permissions
<!--read and write access to contacts--
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
<uses-permission android:name= "Android.permission.WRITE_CONTACTS"/>
35-2: New entity class holds contacts
int c_id;
String C_name;
String C_phone;
String C_email;
Set and get methods and how to construct them
35-3: New URI
Uri uri = contactscontract.contacts.content_uri;//Contact Name
Uri Uriphone = contactscontract.commondatakinds.phone.content_uri;//Contact Phone
Uri uriemail = contactscontract.commondatakinds.email.content_uri;//Contact Mailbox
35-4: New Contentresolver
Contentresolver resolver = This.getcontentresolver ();
35-5: Use the mailing mark to get the contact name
35-5-1: New address, query by mail mark
cursor cursor = resolver.query (URI, NULL, NULL, NULL, NULL);
35-5-2: Start query
int contactidindex = 0;//Contact ID
int nameindex = 0;//Contact person Name
if (Cursor.getcount () >0) {
Contactidindex = Cursor.getcolumnindex (contactscontract.contacts._id);
Nameindex = Cursor.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME);
}
35-5-3: Get the value by MoveToNext ()
while (Cursor.movetonext ()) {
String contactId = cursor.getstring (Contactidindex);
Name = Cursor.getstring (Nameindex);
...... (The following code is also available here)
}
35-6: Use the mailing mark to get the contact number (continue on the basis of the while loop above)
35-6-1: Cursor stamp for new phone number
Selection is equivalent to the WHERE statement in SQL
String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" +contactid;
Cursor phones = this.getcontentresolver (). Query (uriphone,null, selection, NULL, NULL);
int phoneindex = 0;
35-6-2 If the query is made, return to Phoneindex
if (Phones.getcount () >0) {
Phoneindex = Phones.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER);
}
35-6-3: Same as Get value
while (Phones.movetonext ()) {
PhoneNumber = phones.getstring (Phoneindex);
...... (If you also want to get a mailbox, write code here.)
}
35-7: Get Mailbox
35-7-1: New Mailing Mark Query
String Selectionemail = ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" +contactid;
Cursor email = this.getcontentresolver (). query (Uriemail, NULL, selectionemail, NULL, NULL);
35-7-2: Query and return
int emailindex = 0;
if (Email.getcount () >0) {
Emailindex = Email.getcolumnindex (ContactsContract.CommonDataKinds.Email.DATA);
}
35-7-3: Same as Get value
while (Email.movetonext ()) {
Email = email.getstring (Emailindex);
}
If you feel trouble, you can use the following code directly:
Note: Please add permissions and examples
Toast.maketext (Getapplicationcontext (), "Getting Information", 0). Show ();
Contentresolver resolver = This.getcontentresolver ();
cursor cursor = resolver.query (URI, NULL, NULL, NULL, NULL);
int contactidindex = 0;
int nameindex = 0;
Contact Contact;
if (Cursor.getcount () > 0) {
Contactidindex = Cursor.getcolumnindex (contactscontract.contacts._id);
Nameindex = Cursor.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME);
}else{
Toast.maketext (Getapplicationcontext (), "You have no contact information", 0). Show ();
}
while (Cursor.movetonext ()) {
String contactId = cursor.getstring (Contactidindex);
Name = Cursor.getstring (Nameindex);
String selection = contactscontract.commondatakinds.phone.contact_id+ "=" + contactId;
Cursor phones = this.getcontentresolver (). Query (Uriphone, null,selection, NULL, NULL);
int phoneindex = 0;
if (Phones.getcount () > 0) {
Phoneindex = Phones.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER);
}
while (Phones.movetonext ()) {
PhoneNumber = phones.getstring (Phoneindex);
Toast.maketext (Getapplicationcontext (), name+ "= = =" +phonenumber, 0). Show ();
Contact = new Contact (name, phonenumber);
List.add (contact);
}
}
Android Content Provider Summary