Android Get mobile phone record

Source: Internet
Author: User

In the Android development, reading the phone address book in the number is a basic operation, but because of the number of Android version, so the phone address Book operation code is more and more numerous, in this article summarizes.

Android1.5 is the lowest version of the current Android system, first of all for Android1.5 and above (with 2. x,3.x) Code implementation:

Get all the Contacts

Cursor cur = context.getcontentresolver (). Query (

Contacts.People.CONTENT_URI,

Null

Null

Null

Contacts.People.DISPLAY_NAME + "COLLATE localized ASC");

Looping through

if (Cur.movetofirst ()) {

int idcolumn = Cur.getcolumnindex (contacts.people._id);

int displaynamecolumn = Cur.getcolumnindex (Contacts.People.DISPLAY_NAME);

do {

Get the ID number of the contact person

String contactId =cur.getstring (idcolumn);

Get Name of contact person

String disPlayName =cur.getstring (displaynamecolumn);

Get the phone number of the contact person

Cursorphonescur = Context.getcontentresolver (). Query (

Contacts.phones.content_uri,null,

Contacts.phones.person_id+ "=" + contactId, NULL, NULL);

if (Phonescur.movetofirst ()) {

do {

Traverse all the phone numbers

Stringphonetype = phonescur.getstring (phonescur

. Getcolumnindex (Contacts.PhonesColumns.TYPE));

String PhoneNumber =phonescur.getstring (phonescur

. Getcolumnindex (Contacts.PhonesColumns.NUMBER));

Your own logic-handling code

}while (Phonescur.movetonext ());

}

}while (Cur.movetonext ());

}

Cur.close ();

You can use this code to read phone numbers from a mobile address book in various versions of your Android phone, and you can read multiple numbers under a name. However, since the use of this code in the 2.x version of the efficiency is not high, read a little longer, and 2.x is now the mainstream of Android system, at least more than 80% of the Android phone share, so you can use the high version of the API for efficient reading.

The code for reading the Address Book for Android2.0 and above is as follows:

Read the phone's local phone

Contentresolver CR =context.getcontentresolver ();

To get the cursor to start an item in the phone book, you must first MoveToNext ()

Cursor cursor =cr.query (contactscontract.contacts.content_uri,null, NULL, NULL, NULL);

while (Cursor.movetonext ()) {

Get the name index of the contact person

int Nameindex =cursor.getcolumnindex (phonelookup.display_name);

String name = cursor.getstring (Nameindex);

Gets the ID index value of the contact person

String contactId =cursor.getstring (Cursor.getcolumnindex (contactscontract.contacts._id));

Query the phone number of the contact person, similar to query Email,photo

Cursor phone =cr.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL,

contactscontract.commondatakinds.phone.contact_id+ "="

+ contactId, NULL, NULL);//The first parameter is to determine the query phone number, the third parameter is to query the specific person's filter value

A person may have several numbers

while (Phone.movetonext ()) {

String PhoneNumber =phone.getstring (Phone.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER));

Listname.add (name);

Listphone.add (PhoneNumber);

}

Phone.close ();

}

Cursor.close ();

If you need to read the contents of the Address book inside the SIM card, you can read it using: "Content://icc/adn", with the following code:

try{

Intent Intent = new Intent ();

Intent.setdata (Uri.parse ("Content://icc/adn"));

Uri uri = Intent.getdata ();

CONTENTRESOLVERCR = Context.getcontentresolver ();

Cursor cursor =context.getcontentresolver (). Query (URI, NULL, NULL, NULL, or NULL);

if (cursor! = NULL) {

while (Cursor.movetonext ()) {

Get the name index of the contact person

int nameindex = Cursor.getcolumnindex (phonelookup.display_name);

String name = cursor.getstring (Nameindex);

Gets the ID index value of the contact person

String contactId =cursor.getstring (Cursor.getcolumnindex (contactscontract.contacts._id));

Query the phone number of the contact person, similar to query Email,photo

Cursor phone =cr.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL,

contactscontract.commondatakinds.phone.contact_id+ "="

+ contactId, NULL, NULL);//The first parameter is to determine the query phone number, the third parameter is to query the specific person's filter value

A person may have several numbers

while (Phone.movetonext ()) {

String PhoneNumber =phone.getstring (Phone.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER));

Your own logic code

}

Phone.close ();

}

Cursor.close ();

}

}catch (Exception e) {}


The information on the address book is stored in two places, one is a SIM card, one is a local phone,

First, the phone is local:
cursor cursor = getcontentresolver (). query (People.content_uri, NULL,
NULL, NULL, NULL);
while (Cursor.movetonext ()) {
ContactInfo CCI = new ContactInfo ();
Get Name of contact person
int namefieldcolumnindex = Cursor.getcolumnindex (people.name);
Cci.contactname = cursor.getstring (Namefieldcolumnindex);
Get phone number
int numberfieldcolumnindex = Cursor.getcolumnindex (People.number);
Cci.usernumber = cursor.getstring (Numberfieldcolumnindex);
Cci.usernumber = GetNumber (Cci.usernumber);
cci.ischecked = false;
if (Isusernumber (Cci.usernumber)) {
if (! Iscontain (ContactList, Cci.usernumber)) {
if (Isalreadycheck (Wnumstr, Cci.usernumber)) {
Cci.ischecked = true;
Numberstr + = "," + cci.usernumber;
}
Contactlist.add (CCI);
LOG.I ("EoE", "*********" +cci.usernumber);
}
}
}
Cursor.close ();
}

Here's how to get a SIM card:

//Pick up from SIM card
private void Getsimcontact (String add) {
//Read SIM phone number, there are two possible: Content://icc/adn and CONTENT://SIM/ADN
try {
Intent Intent = new Intent ();
Intent.setdata (Uri.parse (add));
Uri uri = Intent.getdata ();
Mcursor = Getcontentresolver (). Query (URI, NULL, NULL, NULL, NULL);
if (mcursor! = null) {
while (Mcursor.movetonext ()) {
ContactInfo sci = new ContactInfo ();
//Get Contact name
int namefieldcolumnindex = Mcursor.getcolumnindex ("name");
Sci.contactname = mcursor.getstring (Namefieldcolumnindex);
//Get phone numbers
int numberfieldcolumnindex = Mcursor
. Getcolumnindex ("number");
Sci.usernumber = Mcursor.getstring (Numberfieldcolumnindex);

Sci.usernumber = GetNumber (Sci.usernumber);
sci.ischecked = false;

if (Isusernumber (Sci.usernumber)) {
if (! Iscontain (ContactList, Sci.usernumber)) {
if (Isalreadycheck (Wnumstr, Sci.usernumber)) {
Sci.ischecked = true;
Numberstr + = "," + sci.usernumber;
}
Contactlist.add (SCI);
LOG.I ("EoE", "*********" +sci.usernumber);
}
}
}
Mcursor.close ();
}
} catch (Exception e) {
LOG.I ("EoE", e.tostring ());
}
}

The above is the information object method that will be obtained arraylist<contactinfo> contactlist inside, then smashing the display is using the adapter, so that the completion of the

When writing, be sure to pay attention to the way you get it.

Android Get mobile phone record

Related Article

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.