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

Posted

技术标签:

【中文标题】从联系人选择器意图中选择电子邮件、姓名和电话号码【英文标题】:Pick email, name, and phone number from contact picker intent 【发布时间】:2020-03-01 13:37:18 【问题描述】:

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

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

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

提前谢谢你!

【问题讨论】:

【参考方案1】:

问题是您在代码中使用了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, 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();

【讨论】:

【参考方案2】:

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

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

见下图输出

【讨论】:

此代码错误 (a) 不响应使用联系人选择器的请求,允许用户选择特定联系人,并且 (b) 它并没有真正查询电话号码,而是它说number is: &lt;an email&gt;

以上是关于从联系人选择器意图中选择电子邮件、姓名和电话号码的主要内容,如果未能解决你的问题,请参考以下文章

如何从联系人中获取电子邮件和姓氏?

Android 联系人选择器获取姓名 + 号码 + 电子邮件

android获取联系人图片、姓名和手机号码

从电话联系人中获取姓名、电话号码和电子邮件地址[关闭]

从联系人选择器中获取号码

如何将联系人姓名、号码、contact_id 保存到 ArrayList / 联系人选择器