Android reads address book

Source: Internet
Author: User

Private arraylist <contactbean> getallcontacts (){
Arraylist <contactbean> arraylist = new arraylist <contactbean> ();

Phonemap = new hashmap <string, Boolean> ();
// Obtain the local contact
Cursor cur = getcontentresolver (). Query (
Contactscontract. Contacts. content_uri,
Null,
Null,
Null,
Contactscontract. Contacts. display_name
+ "Collate localized ASC ");
If (cur. moveToFirst ()){
Do {
ContactBean samContact = new ContactBean ();
SamContact. setSelected (false );
Int idColumn = cur. getColumnIndex (ContactsContract. Contacts. _ ID );
Int displayNameColumn = cur. getColumnIndex (ContactsContract. Contacts. DISPLAY_NAME );
// Obtain the contact ID
String contactId = cur. getString (idColumn );
// Obtain the contact name
String displayname = cur. getstring (displaynamecolumn );
System. Out. println (displayname );
Samcontact. contactname = displayname;
// Check the contact's telephone number. If no value is returned, the value 0 is returned.
Int phonecount = cur. getint (cur. getcolumnindex (contactscontract. Contacts. has_phone_number ));
If (phonecount <1 ){
Continue;
}
Cursor phones = getcontentresolver (). Query (
Contactscontract. commondatakinds. Phone. content_uri,
Null,
Contactscontract. commondatakinds. Phone. contact_id
+ "=" + Contactid, null, null );
If (phones! = NULL & phones. movetofirst ()){
Do {
// Traverse all phone numbers
String phonenumber = phones
. Getstring (Phones
. Getcolumnindex (contactscontract. commondatakinds. Phone. Number ));
Phonenumber = phonenumber. Trim ();
Phonenumber = phonenumber. replaceall ("","");
Int phonetype = phones
. GetInt (phones
. GetColumnIndex (ContactsContract. CommonDataKinds. Phone. TYPE ));
If (phoneType = ContactsContract. CommonDataKinds. Phone. TYPE_MOBILE ){
Boolean hasAddPhoneNumber = false;
HasAddPhoneNumber = phoneMap. get (phoneNumber );
If (hasAddPhoneNumber! = Null & hasAddPhoneNumber ){
Break;
} Else {
SamContact. contactPhoneNumber = phoneNumber;

// Set the selected
If (defaselecselectlist! = Null ){
Int count = defaultSelectList. size ();
For (int kk = 0; kk <count; kk ++ ){
If (defaselecselectlist. get (kk). getContactPhoneNumber (). equals (samContact. getContactPhoneNumber ())){
SamContact. setSelected (true );
Break;
}
}
}

ArrayList. add (samContact );
PhoneMap. put (phoneNumber, true );
}
Break;
}
} While (phones. moveToNext ());

If (phones! = Null ){
Phones. close ();
}

}

} While (cur. moveToNext ());
}

If (cur! = Null ){
Cur. close ();
}
// Get the SIM card contact
Uri uri = Uri. parse ("content: // icc/adn ");
Cursor cur2 = getContentResolver (). query (
Uri,
Null,
Null,
Null,
ContactsContract. Contacts. DISPLAY_NAME
+ "Collate localized asc ");
// System. out. println ("contact num from sim card =" + cur2.getCount ());
// System. out. println ("---------------");
If (cur2! = Null & cur2.moveToFirst ()){
Do {
Try {
ContactBean samContact = new ContactBean ();
SamContact. setSelected (false );
Int displayNameColumn = cur2.getColumnIndex (Contacts. People. NAME );
Int phoneColumn = cur2.getColumnIndex (Contacts. People. NUMBER );

String phoneNumber = cur2.getString (phoneColumn );
Phonenumber = phonenumber. Trim ();
Phonenumber = phonenumber. replaceall ("","");
Samcontact. contactname = cur2.getstring (displaynamecolumn );
If (samcontact. contactname = NULL ){
Continue;
}
Samcontact. contactphonenumber = phonenumber;
If (samcontact. contactphonenumber = NULL ){
Continue;
}
Boolean hasAddPhoneNumber = false;
HasAddPhoneNumber = phoneMap. get (samContact. contactPhoneNumber );
If (hasAddPhoneNumber! = Null & hasAddPhoneNumber ){
Continue;
} Else {

// Set the selected
If (defaselecselectlist! = Null ){
Int count = defaultSelectList. size ();
For (int kk = 0; kk <count; kk ++ ){
If (defaselecselectlist. get (kk). getContactPhoneNumber (). equals (samContact. getContactPhoneNumber ())){
SamContact. setSelected (true );
Break;
}
}
}

ArrayList. add (samContact );
PhoneMap. put (samContact. getContactPhoneNumber (), true );
}
ArrayList. add (samContact );
} Catch (Exception e ){
E. printStackTrace ();
}
} While (cur2.moveToNext ());
}

If (cur2! = Null ){
Cur2.close ();
}

Return arrayList;
}

Public class ContactBean implements Parcelable {
Public String contactName;
Public String contactPhoneNumber;
Private boolean isSelected;

Public boolean isSelected (){
Return isSelected;
}

Public void setSelected (boolean selected ){
IsSelected = selected;
}

Public String getContactPhoneNumber (){
Return contactPhoneNumber;
}

Public void setContactPhoneNumber (String contactPhoneNumber ){
This. contactPhoneNumber = contactPhoneNumber;
}

Public String getContactName (){
Return contactName;
}

Public void setContactName (String contactName ){
This. contactName = contactName;
}

@ Override
Public int describeContents (){
Return 0;
}

@ Override
Public void writeToParcel (Parcel parcel, int I ){
Int isChecked = isSelected? 1:0;
Parcel. writeInt (isChecked );
Parcel. writeString (contactName );
Parcel. writeString (contactPhoneNumber );
}

Public static final Parcelable. Creator CREATOR = new Creator (){

@ Override
Public ContactBean createFromParcel (Parcel source ){

// TODO Auto-generated method stub
ContactBean parcelableObj = new ContactBean ();
ParcelableObj. isSelected = source. readInt () = 1? True: false;
ParcelableObj. contactName = source. readString ();
ParcelableObj. contactPhoneNumber = source. readString ();
Return parcelableObj;

}

@ Override
Public ContactBean [] newArray (int size ){

Return new ContactBean [size];

}

};
}

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.