2014-01-22 148 views
-5

我需要創建一個項目,以從電話默認聯繫人列表中選擇聯繫人中的Name,Phone Number and Email Address
 步驟:
   當我點擊我的應用程序中的按鈕,它會打開或轉到默認的手機聯繫人。之後,我選擇一個手機聯繫人,它會回到我的應用程序,並有3個文本視圖來放置姓名,電子郵件和電話號碼。我如何實現這一目標?
Thanx提前從電話聯繫人中獲取姓名,電話號碼和電子郵件地址

+0

嘗試一些內容並顯示您的代碼。不要指望別人只是爲你做你的工作 – geedubb

回答

2

我做這種方式爲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 
+0

它的工作感謝您的幫助 –

+0

wl來兄弟.... –

相關問題