从联系人选择器意图中选择电子邮件,姓名和电话号码

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

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

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

提前谢谢!

jjjjj662002 回答:从联系人选择器意图中选择电子邮件,姓名和电话号码

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

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

有关输出,请参见下图 See The Below Output

,

问题在于,通过在代码中使用Phone-picker,您可以通过设置setType(Phone.CONTENT_TYPE)告诉通讯录应用程序您仅对电话号码感兴趣。

您需要像这样切换到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);
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);
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();
本文链接:https://www.f2er.com/3161339.html

大家都在问