获取姓名,电话号码和电子邮件地址从手机通讯录 [英] Getting Name,Phone Number and Email Address From Phone Contacts

查看:274
本文介绍了获取姓名,电话号码和电子邮件地址从手机通讯录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个项目,获得姓名,电话号码和电子邮件地址从手机预设的联系人列表中选定联系人。结果
 步骤:结果
   当我在我的应用程序点击一个按钮,它会打开或到预设的电话联系。从那以后,我选择手机通讯录它会回来给我的应用程序,并有3文本视图放置姓名,电子邮件和电话号码。我怎样才能做到这一点?结果
 感谢名单提前

I needed to Create a project that getting the Name,Phone Number and Email Address from a selected contact from the Phones Default contact list.
 Steps:
  When I clicked a button in my application it will open or go to the default phone contact. After that I select a phone contact it will comes back to my application and there is 3 text View to place the Name,Email and phone number. How can i Achieve this?
Thanx in advance

推荐答案

我做这种方式为的Andr​​oid 2.2 Froyo 版本:基本使用日食来创建一个,如:公共类SomePickContactName扩展活动

I do it this way for Android 2.2 Froyo release: basically use eclipse to create a class like: public class SomePickContactName extends Activity

然后将这个code。记得要加在我的版本code的引用的私有类变量和常量:

then insert this code. Remember to add the private class variables and CONSTANTS referenced in my version of the code:

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

这篇关于获取姓名,电话号码和电子邮件地址从手机通讯录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆