Two ways to get phone contacts

Source: Internet
Author: User
two ways to get phone contacts


The first method is simpler, just a few words, but the information is not perfect.

The second method is more difficult than the code to get more information full


Required Privileges:

<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
Import java.util.ArrayList;
Import java.util.List;

Import android.app.Activity;
Import Android.content.ContentResolver;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.provider.ContactsContract;
Import Android.view.View;

public class Mainactivity extends activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
}


public void Btnreadcontacts (View v) {

StringBuffer sb = Readcontact ();

list<contacts> Contacts = Readcontact ();

SYSTEM.OUT.PRINTLN ("Contact information:" +contacts.tostring ());


}


Read contacts remember to add permissions
public void Btnotherreadcontacts (View v) {

The parser object for the content provider
Contentresolver resolver = Getcontentresolver ();
This loop is the data table.
Cursor Cursor = resolver.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL, NULL, NULL, NULL);

StringBuffer sb = new StringBuffer ();

while (Cursor.movetonext ()) {

String name = cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phone = cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER));

Sb.append ("Name:" +name+ "Tel:" +phone);
}


SYSTEM.OUT.PRINTLN ("---:" +sb.tostring ());


}





/**
* Read system Contacts
* @return return information for all contact persons
*/
Private List<contacts> readcontact () {
Gets a content provider parser object as a mobile phone
Contentresolver resolver = Getcontentresolver ();

This is the query raw_contacts
Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");

This is the data table for the query
Uri Datauri = Uri.parse ("Content://com.android.contacts/data");


SELECT * from Raw_contacts;
Cursor rawcursor = resolver.query (URI, NULL, NULL, NULL, NULL);


Create a container that holds contacts
StringBuffer sb = new StringBuffer ();
list<contacts> contactslist = new arraylist<contacts> ();

while (Rawcursor.movetonext ()) {

Contacts Contacts = new Contacts ();

Get ID of contact person
int id = rawcursor.getint (rawcursor.getcolumnindex ("contact_id"));


SELECT * FROM data where raw_contact_id = ID?
Cursor datacursor = resolver.query (Datauri, null, "raw_contact_id =?", New String[]{id+ ""}, NULL);

Gets the name of the column returned by the specified table
/*string[] names = Datacursor.getcolumnnames ();

for (int i = 0; i < names.length; i++) {

SYSTEM.OUT.PRINTLN ("Column name->" +names[i]);

}*/

while (Datacursor.movetonext ()) {

Get a contact's data phone, mailbox
String content = datacursor.getstring (Datacursor.getcolumnindex ("data1"));
String mimetype = datacursor.getstring (Datacursor.getcolumnindex ("mimetype"));

Used to determine exactly what kind of data a data is.
if ("Vnd.android.cursor.item/phone_v2". Equals (MimeType)) {
Sb.append ("Tel:" +content);
Contacts.setphone (content);
}else if ("Vnd.android.cursor.item/email_v2". Equals (MimeType)) {
Sb.append ("Mailbox:" +content);
Contacts.setemial (content);
}else if ("Vnd.android.cursor.item/name". Equals (MimeType)) {
Sb.append ("Name:" +content+ "\ n");
Contacts.setname (content);
}
}
Contactslist.add (contacts);
}
return contactslist;
}







}

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.