3

如何正确获取电子邮件地址?这是我到目前为止所做的。

@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, 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);
     }
  }
}

上面的代码正确地获取了姓名和电话号码,但电子邮件作为电话号码返回,而不是联系人的电子邮件地址。

先感谢您!

4

2 回答 2

2

问题是您Phone-picker在代码中使用 a ,通过设置setType(Phone.CONTENT_TYPE)您告诉联系人应用您只对电话号码感兴趣。

您需要切换到 a Contact-picker,如下所示:

Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);

然后你读到这样的结果:

Uri contactData = data.getData();

// get contact-ID and name
String[] projection = new String[] { Contacts._ID, Contacts.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
cursor.moveToFirst();
long id = cursor.getLong(0);
String name = cursor.getString(1);
Log.i("Picker", "got a contact: " + id + " - " + name);
cursor.close();

// get email and phone using the contact-ID
String[] projection2 = new String[] { Data.MIMETYPE, Data.DATA1 };
String selection2 = Data.CONTACT_ID + "=" + id + " AND " + Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "')";
Cursor cursor2 = getContentResolver().query(Data.CONTENT_URI, projection2, selection2, null, null);
while (cursor2 != null && cursor2.moveToNext()) {
    String mimetype = cursor2.getString(0);
    String data = cursor2.getString(1);
    if (mimetype.equals(Phone.CONTENT_ITEM_TYPE)) {
        Log.i("Picker", "got a phone: " + data);
    } else {
        Log.i("Picker", "got an email: " + data);
    }
}
cursor2.close();
于 2019-11-06T10:00:35.957 回答
0

您可以使用以下方法获取联系号码、电子邮件和联系人姓名

 public static  void getContactDetails(Context context){
    ArrayList<String> names = new ArrayList<String>();
    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor cur1 = cr.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                    new String[]{id}, null);
            while (cur1.moveToNext()) {

                String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String number=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e("Contact Details","\n Name is :"+name+" Email is: "+ email+ " Number is: "+number);

                if(email!=null){
                    names.add(name);
                }
            }
            cur1.close();
        }
    }

见下图输出 请参阅下面的输出

于 2019-11-06T06:19:14.907 回答