如何调用Android的联系人列表,并从它的细节画面中选择一个电话号码?它的、并从、电话号码、画面

2023-09-12 07:19:27 作者:雨夜星沙

我已经阅读了已发布的解决方案,但他们不知道如何使用系统的联系信息屏幕选择要使用的任何一个数字? 我开发一个短信发送Android应用程序它提供了可供选择的手机和用户想要使用发送到......

I have read the already posted solutions, but they dont tell how do I use system's contact details screen to select any ONE number to use? I am developing an sms sending android app which offers to choose contacts of the phone and the number a user wants to use to send to....

到目前为止,我一直没能找到有关选择任意一个数字事情。它是否只需要通过程序来完成?检索所有号码从数据库和发送短信了吗?

So far I have not been able to find anything about choosing any one number. Does it only has to be done programatically? retrieving all numbers from database and sending sms to it?

问候

雪利酒

推荐答案

唷,我花了一些时间,但我想我有你需要的(即使它的为时已晚已经是答案,但我还是会后它作为其他基准)。

phew, it took me some time, but i think i have the answer you need (even if it's too late already, but i'll still post it as a reference for others).

在应用程序中我目前正在开发用户可以输入一个电话号码, 一个的EditText 或点击一个按钮,选择一个人从手机地址簿。如果这个人有多个电话号码,有一个下拉列表,在那里他可以准确地选择其中一个。

in the application i'm currently developing the user may enter a phone number into an EditText or click on a button and select a person from the phones address book. if the person has more than one phone number, there's a drop down list where he can select exactly one of them.

@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);                
        }
    });
}

现在,只要用户选择一个联系人(可能选择的多个电话号码的),你可以检索数据的正常方式:

now, as soon as the user selects a contact (and probably chooses one of several phone numbers), you can retrieve the data the normal way:

@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对于CommonDataKinds.Phone对dev.android 。

整型类型告诉你号码类型:移动(2),家庭(1),工作(3),等等

the int "type" tells you the type of number: mobile (2), home (1), work (3), and so on.

请注意:在用户选择的接触,他得到的数字的微调,没有显示号码的类型。这不是真正的用户友好:?如果联系人有5个编有号码......呃,这其中的一个是传真号码再次

note: after the user selects the contact, he gets a spinner of numbers with no indication of the numbers type. that's not really user friendly: if a contact has 5 assigned numbers ... uh, which one of these is the fax number again?

另注:上面的例子中所需要的SDK> 5(安卓2.0+),所以没有1.6(= SDK 4)。 1.6具有不同的API,如果你想支持两个版本,阅读有关dev.android联系人API的文章。

another note: above example needs the sdk > 5 (Android 2.0+), so no 1.6 (=sdk 4). 1.6 has a different api, and if you want to support both versions, read the article about the contacts API on dev.android.

好运。

声明:我抄我的大多数code OUT的PickContact.java例如

disclaimer: i copied most of my code out of the PickContact.java example