【问题标题】:Getting Name,Phone Number and Email Address From Phone Contacts [closed]从电话联系人中获取姓名、电话号码和电子邮件地址[关闭]
【发布时间】:2014-02-12 01:11:50
【问题描述】:

我需要创建一个项目,从“电话默认”联系人列表中的选定联系人那里获取 Name,Phone Number and Email Address
步骤:
当我单击应用程序中的按钮时,它将打开或转到默认电话联系人。之后我选择一个电话联系人,它将返回到我的应用程序,并且有 3 个文本视图来放置姓名、电子邮件和电话号码。我怎样才能做到这一点?
提前感谢

【问题讨论】:

  • 尝试一下并展示您的代码。不要指望别人只是为你做你的工作

标签: android email button contacts android-contacts


【解决方案1】:

我这样做是为了Android 2.2 Froyo 发布:基本上使用eclipse 来创建class,例如:public class SomePickContactName extends Activity

然后插入此代码。记得添加我的代码版本中引用的私有类变量和CONSTANTS:

protected void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState);       
  Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
  startActivityForResult(intentContact, PICK_CONTACT);
}//onCreate

public void onActivityResult(int requestCode, int resultCode, Intent intent) 
{

  if (requestCode == PICK_CONTACT)
  {         
    getContactInfo(intent);         
    // Your class variables now have the data, so do something with it. 
  }
}//onActivityResult

protected void getContactInfo(Intent intent)
{

   Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
   while (cursor.moveToNext()) 
   {           
       String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
       name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

       String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

       if ( hasPhone.equalsIgnoreCase("1"))
           hasPhone = "true";
       else
           hasPhone = "false" ;

       if (Boolean.parseBoolean(hasPhone)) 
       {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
        while (phones.moveToNext()) 
        {
          phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        }
        phones.close();
       }

       // Find Email Addresses
       Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);
       while (emails.moveToNext()) 
       {
        emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
       }
       emails.close();

    Cursor address = getContentResolver().query(
                ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId,
                null, null);
    while (address.moveToNext()) 
    { 
      // These are all private class variables, don't forget to create them.
      poBox      = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
      street     = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
      city       = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
      state      = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
      postalCode = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
      country    = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
      type       = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
    }  //address.moveToNext()   
  }  //while (cursor.moveToNext())        
   cursor.close();
}//getContactInfo

【讨论】:

  • 成功了,谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2018-03-16
  • 2015-06-01
  • 1970-01-01
  • 2014-10-16
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多