从 Android 内置电话簿获取电子邮件 ID 和姓名?

发布于 2024-12-17 15:48:57 字数 1610 浏览 1 评论 0原文

我正在尝试将 Android 内置电话簿中的姓名和电子邮件 ID 获取到我的页面中,我能够获取姓名、联系人 ID、电话号码。但我无法从 Android 电话簿获取电子邮件 ID。

代码是:

 public static final int PICK_CONTACT = 1;
 @Override
 button.setOnClickListener(new OnClickListener() {
 public void onClick(View _view) {
 Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
 startActivityForResult(intent, PICK_CONTACT);  
   } 
 });
}

@Override 
public void onActivityResult(int reqCode, int resCode, Intent data) 
{
 super.onActivityResult(reqCode, resCode, data);
    
switch(reqCode) {
  case (PICK_CONTACT) : {
    if (resCode == Activity.RESULT_OK) {
      Uri contactData = data.getData();
      Cursor c = managedQuery(contactData, null, null, null, null);
      c.moveToFirst();
      String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
      String name1 = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
      String ContactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

   if(Integer.parseInt(name1) == 1){
     Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
     ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + ContactID, null, null);
     TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
     TextView tv1 = (TextView)findViewById(R.id.selected_email_textview);
     tv.setText(name);
     tv1.setText(ContactID);
      }
      }
        break;
  }
 } 

在这里我可以从电话簿中获取所选人员的姓名和联系ID。现在我想从电话簿中获取所选人员的姓名和电子邮件 ID。 我怎样才能实现这个目标?

I am trying to get name and email-id from Android inbuilt phone book into my page, I am able to get name, contact ID, phone number. but I am unable to get email ID from the Android phone book.

Code is:

 public static final int PICK_CONTACT = 1;
 @Override
 button.setOnClickListener(new OnClickListener() {
 public void onClick(View _view) {
 Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
 startActivityForResult(intent, PICK_CONTACT);  
   } 
 });
}

@Override 
public void onActivityResult(int reqCode, int resCode, Intent data) 
{
 super.onActivityResult(reqCode, resCode, data);
    
switch(reqCode) {
  case (PICK_CONTACT) : {
    if (resCode == Activity.RESULT_OK) {
      Uri contactData = data.getData();
      Cursor c = managedQuery(contactData, null, null, null, null);
      c.moveToFirst();
      String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
      String name1 = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
      String ContactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

   if(Integer.parseInt(name1) == 1){
     Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
     ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + ContactID, null, null);
     TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
     TextView tv1 = (TextView)findViewById(R.id.selected_email_textview);
     tv.setText(name);
     tv1.setText(ContactID);
      }
      }
        break;
  }
 } 

Here I am able to get name and contact ID of a selected person from the phonebook. Now I want to get name and email ID of a selected person from phone book.
How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰ツ都不明白 2024-12-24 15:48:57

您可以直接使用联系人 ID 获取联系人的电子邮件地址,如下所示:

    String selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID + " =? ";
    String[] selectionArgs = new String[]{aContactId};
    Cursor emailCursor = aContext.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, selection, selectionArgs, null);
    while (!emailCursor.isAfterLast())
    {
        emailString = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
// do something with email string
        emailCursor.moveToNext();
    }
    emailCursor.close ();

You can get the email address of the contact directly using the contact id as follows :

    String selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID + " =? ";
    String[] selectionArgs = new String[]{aContactId};
    Cursor emailCursor = aContext.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, selection, selectionArgs, null);
    while (!emailCursor.isAfterLast())
    {
        emailString = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
// do something with email string
        emailCursor.moveToNext();
    }
    emailCursor.close ();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文