Android Cool Choose Contact Error

Source: Internet
Author: User

Encountered a bug, the cool mobile phone when executing the following code will be error

Intent i = new Intent (Intent.action_pick);                I.settype ("Vnd.android.cursor.dir/phone");                Startactivityforresult (i, selectnumber);


protected void Onactivityresult (int requestcode, int resultcode, Intent dataintent) {
Super.onactivityresult (Requestcode, ResultCode, dataintent);
if (Requestcode = = Selectnumber && ResultCode = = RESULT_OK) {
if (dataintent = = null) {
Return
}
Uri uri = Dataintent.getdata ();
cursor cursor = getcontentresolver (). Query (URI, NULL, NULL, NULL, or NULL);

if (Cursor.movetofirst ()) {
       Name.settext (Cursor.getstring (Cursor.getcolumnindexorthrow (Contacts.Phones.NAME)) + "");
Phone.settext (Cursor.getstring (Cursor.getcolumnindexorthrow (Contacts.Phones.NUMBER)) + "");
if (Build.VERSION.SDK_INT < 14) {
Cursor.close ();
}
}
}

This should be caused by not implementing the contact information returned with the phone. Comment out I.settype ("Vnd.android.cursor.dir/phone"); can function correctly, but the

Onactivityresult cannot get the contact name and phone through the cursor.

After the online check to read the contact ID first, and then to find the phone number, so a change of code bloated a lot, but to be compatible with the cool. The code is as follows:
Intent Intent = new Intent (Intent.action_pick, ContactsContract.Contacts.CONTENT_URI);                if (Getpackagemanager (). Resolveactivity (Intent, 0)! = null) {                    Startactivityforresult (intent, Selectnumber);                } else {                    showmsg ("Your phone does not support selecting Contacts Contacts");                }

protected void Onactivityresult (int requestcode, int resultcode, Intent dataintent) {
Super.onactivityresult (Requestcode, ResultCode, dataintent);
if (Requestcode = = Selectnumber && ResultCode = = RESULT_OK) {
if (dataintent = = null) {
Return
}
Uri contactdata = Dataintent.getdata ();
cursor cursor = managedquery (contactdata, NULL, NULL, NULL, or NULL);
String id = "";
if (Cursor.movetofirst ()) {
Name.settext (Cursor.getstring (Cursor.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME)) + "");// Name is TextView
String Haanumber = cursor.getstring (Cursor.getcolumnindex (ContactsContract.Contacts.HAS_PHONE_NUMBER));
id = cursor.getstring (cursor.getcolumnindex (contactscontract.contacts._id));
if (Haanumber.equalsignorecase ("1")) {
Cursor Phones =getcontentresolver (). query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ID, NULL, NULL);
while (Phones.movetonext ()) {
Phone.settext (Phones.getstring (Phones.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER));// Phone for TextView
                }
if (Build.VERSION.SDK_INT < 14) {
Phones.close ();
}
}
if (Build.VERSION.SDK_INT < 14) {
Cursor.close ();
}
}
}
}

  

Android Cool Choose Contact Error

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.