如何正确获取电子邮件地址?这是我到目前为止所做的。
@android.webkit.JavascriptInterface
public void chooseContact(){
Intent pickContactIntent = new Intent(Intent.actION_PICK);
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startactivityForResult(pickContactIntent,CONTact_PICKER_RESULT);
}
@Override
protected void onactivityResult(int requestCode,int resultCode,Intent data){
if (requestCode == CONTact_PICKER_RESULT){
Uri contacturi = data.getData();
// Perform the query.
// We don't need a selection or sort order (there's only one result for the given URI)
Cursor cursor = getcontentResolver().query(contacturi,null,null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMber column.
int column = cursor.getcolumnIndex(ContactsContract.CommonDataKinds.Phone.NUMber);
String phoneNumber = cursor.getString(column);
if(phoneNumber != null)
phoneNumber = phoneNumber.trim();
if(phoneNumber == null || phoneNumber.equals("")) {
// This should never happen,but just in case we'll handle it
Log.e(TAG,"Phone number was null!");
Util.debugAsserts(false);
return;
}
// Retrieve the contact name.
column = cursor.getcolumnIndex(ContactsContract.CommonDataKinds.Identity.DISPLAY_NAME);
String name = cursor.getString(column);
// Retrieve the contact email address.
column = cursor.getcolumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
String email = cursor.getString(column);
cursor.close();
JSONObject contacts = new JSONObject();
String jsonString;
try {
contacts.put("name",name);
contacts.put("email",email);
contacts.put("phoneNumber",phoneNumber);
jsonString = contacts.toString();
String encodedData = Base64.encodeToString(jsonString.getBytes(),Base64.DEFAULT);
String command = String.format("get_contact_details('%s');",encodedData);
eaWebView.submitJavascript(command);
} catch (JSONException e) {
throw new Error(e);
}
}
}
上面的代码正确获取了姓名和电话号码,但是电子邮件返回的是电话号码,而不是联系人的电子邮件地址。
提前谢谢!