1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String email = "";
String name = "";
try {
/* A TESTER */
Uri contactData = data.getData();
Cursor c = getContentResolver().query(Data.CONTENT_URI, new String[] { Phone.NUMBER }, null, null, null);
if (c.moveToFirst()) {
String[] n = cursor.getColumnNames() ;
for(int i = 0; i < n.length; i++) {
Log.v("==>", "Got " + n[i] + ": " + cursor.getString(cursor.getColumnIndex(n[i])));
}
// String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
Uri result = data.getData();
Log.v("==>", "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID + "=?", new String[] { id }, null) ;
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",new String[] { id },null);
// let's just get the first email
if (cursor.moveToFirst()) {
String[] n = cursor.getColumnNames() ;
for(int i = 0; i < n.length; i++) {
Log.v("==>", "Got " + n[i] + ": " + cursor.getString(cursor.getColumnIndex(n[i])));
}
} else {
Log.w("==>", "No results");
}
if (phones.moveToFirst()) {
String[] n = phones.getColumnNames() ;
for(int i = 0; i < n.length; i++) {
Log.v("==>", "Got " + n[i] + ": " + phones.getString(phones.getColumnIndex(n[i])));
}
} else {
Log.w("==>", "No results");
}
} catch (Exception e) {
Log.e("==>", "Failed to get email data", e);
} finally {
if (cursor != null) {
cursor.close();
}
fEmail.setText(email);
if (email.length() == 0) {
Toast.makeText(this, "No email found for contact.",
Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
// gracefully handle failure
Log.w("==>", "Warning: activity result not ok");
}
} |
Partager