2011-02-14 64 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); 
相关问题