2011-02-14 44 views
26

我已閱讀已發佈的解決方案,但他們不知道如何使用系統的聯繫人詳細信息屏幕來選擇要使用的任何一個號碼? 我正在開發一個短信發送Android應用它提供了可供選擇的手機和用戶想要使用發送到號碼的聯繫人....如何調用Android聯繫人列表並從其詳細信息屏幕中選擇一個電話號碼?

到目前爲止,我一直沒能找到有關選擇任何一個東西數。它只需要以編程方式完成?檢索數據庫中的所有數字併發送短信給它?

問候

雪利酒

+0

嗨,你有什麼辦法解決?因爲我也在尋找同樣的東西。我可以使用`new intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI)'來選擇電話號碼;`但是該屏幕不顯示我需要的照片。 – Vasu 2011-12-22 13:55:03

+0

檢查此鏈接http://stackoverflow.com/a/9798765/1282492 – Akiff 2012-07-30 01:28:03

回答

55

唷,我花了一些時間,但我想我有你需要的(即使它是爲時已晚已經是答案,但我還是會發布它作爲一個參考其他)。

在應用程序我目前正在開發用戶可能會輸入一個電話號碼到 和EditText或點擊一個按鈕,並從電話地址簿中選擇一個人。如果該人有多個電話號碼,則有一個下拉列表,他可以正確選擇其中的一個。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.contact_picker); 

    // this opens the activity. note the Intent.ACTION_GET_CONTENT 
    // and the intent.setType 
    ((Button)findViewById(R.id.pick_person)).setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser 
      Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      // BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE 
      intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
      startActivityForResult(intent, 1);     
     } 
    }); 
} 

現在,只要用戶選擇的聯繫人(也可能選擇多個電話號碼之一),你可以檢索數據的正常方式:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (data != null) { 
     Uri uri = data.getData(); 

     if (uri != null) { 
      Cursor c = null; 
      try { 
       c = getContentResolver().query(uri, new String[]{ 
          ContactsContract.CommonDataKinds.Phone.NUMBER, 
          ContactsContract.CommonDataKinds.Phone.TYPE }, 
         null, null, null); 

       if (c != null && c.moveToFirst()) { 
        String number = c.getString(0); 
        int type = c.getInt(1); 
        showSelectedNumber(type, number); 
       } 
      } finally { 
       if (c != null) { 
        c.close(); 
       } 
      } 
     } 
    } 
} 

public void showSelectedNumber(int type, String number) { 
    Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();  
} 

這裏的documentation for CommonDataKinds.Phone for on dev.android

int「type」告訴你數字的類型:mobile(2),home(1),work(3)等等。

注意:在用戶選擇聯繫人之後,他得到一個數字微調,沒有指示數字類型。這不是真正的用戶友好:如果聯繫人有5個分配的號碼......呃,這些又是哪個傳真號碼?

另一個說明:上面的例子需要sdk> 5(Android 2.0+),所以沒有1.6(= sdk 4)。 1.6有不同的API,如果你想支持這兩個版本,請閱讀article about the contacts API on dev.android

祝你好運。

聲明:我複製了我大部分的代碼了PickContact.java example

+6

有了ACTION_GET_CONTENT和CONTENT_ITEM_TYPE,你有時會在選擇器中獲得其他應用程序(例如DropBox和OI文件管理器),這可能是不可取的。使用ACTION_PICK和CONTENT_TYPE可以解決這個問題。 – BoD 2013-02-26 14:26:41

+0

謝謝,我更新了示例源代碼。 – stefs 2013-02-27 14:37:00

3

這個代碼只是正常工作與我

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 

    Intent i = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); 
    super.startActivityForResult(i, 1001); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    switch (requestCode) { 
    case 1001: 

     if (resultCode == Activity.RESULT_OK) { 

      Cursor s = getContentResolver().query(Phone.CONTENT_URI, null, 
        null, null, null); 

     if (s.moveToFirst()) { 
       String phoneNum = s.getString(s.getColumnIndex(Phone.NUMBER)); 
       Toast.makeText(getBaseContext(), phoneNum, Toast.LENGTH_LONG).show(); 
          } 

     } 

     break; 

    } 

} 
2

試試這個代碼,我相信它會工作

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
      startActivityForResult(intent, PICK_CONTACT); 
0

TRY THIS

setContentView(R.layout.main); 

    contactNumber = (TextView)findViewById(R.id.contactnumber); 

    Button buttonPickContact = (Button)findViewById(R.id.pickcontact); 
    buttonPickContact.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
    // TODO Auto-generated method stub 


    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
    startActivityForResult(intent, 1);    


    }}); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if(requestCode == RQS_PICK_CONTACT){ 
    if(resultCode == RESULT_OK){ 
    Uri contactData = data.getData(); 
    Cursor cursor = managedQuery(contactData, null, null, null, null); 
    cursor.moveToFirst(); 

     String number =  cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

     //contactName.setText(name); 
     contactNumber.setText(number); 
     //contactEmail.setText(email); 
    } 
    } 
    } 
    } 

編輯XML

<?xml version="1.0" encoding="utf-8"?> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <Button 
     android:id="@+id/pickcontact" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Pick Contact" /> 

     <TextView 
     android:id="@+id/contactnumber" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
2

接受的答案中存在問題。 如果所選號碼中包含空格
即85 29 948789 那麼它將只顯示85(直到第一個空格)。

因此使用下面的代碼來糾正這個問題:)

Intent intent1 = new Intent(Intent.ACTION_PICK); 
intent1.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); 
      startActivityForResult(intent1, 1); 

和onActivityResult

Uri contactUri = data.getData(); 

    String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}; 


    Cursor cursor = getContentResolver() 
      .query(contactUri, projection, null, null, null); 
    cursor.moveToFirst(); 


    int numberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
    int nameColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
    String number = cursor.getString(numberColumn); 
    String name = cursor.getString(nameColumn); 
相關問題