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