【问题标题】:Contact intent with multiple select多选联系意图
【发布时间】:2015-10-22 19:56:27
【问题描述】:

我正在尝试打开联系人意图并让用户选择多个联系人
意图调用:

Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts / phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);

使用这种方法,用户只能选择一个联系人..我怎样才能让他选择很多联系人,然后得到他选择的所有号码?

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    有两个是这样做的!

    1.在intent中设置MAX pick limit:

    public static final int REQUEST_CODE_PICK_CONTACT = 1;
    public static final int  MAX_PICK_CONTACT= 10;
    
    private void launchMultiplePhonePicker() { 
    
        Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
        phonebookIntent.putExtra("additional", "phone-multi");
        phonebookIntent.putExtra("maxRecipientCount", MAX_PICK_CONTACT);
        phonebookIntent.putExtra("FromMMS", true);
        startActivityForResult(phonebookIntent, REQUEST_CODE_PICK_CONTACT);
    
     }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        if(resultCode==RESULT_OK) {
            if(requestCode == REQUEST_CODE_PICK_CONTACT) {
    
                Bundle bundle =  data.getExtras();
    
                String result= bundle.getString("result");
                ArrayList<String> contacts = bundle.getStringArrayList("result");
    
                Log.i(TAG, "launchMultiplePhonePicker bundle.toString()= " + contactsPick.toString() );
            }
        }
    
        super.onActivityResult(requestCode, resultCode, data);
    }
    

    注意:在每个操作系统版本和设备上可能会有所不同,并且可能无法在所有设备上运行。


    1. 这样做:

      以编程方式读取联系人并将它们显示在 Activity 的 ListView 中,然后在 ListView 项目中使用 Checkboxes 并允许选择多个项目。

    例如如何读取系统联系人:

    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        if("1".equals(hasPhone) || Boolean.parseBoolean(hasPhone)) { 
            // You know it has a number so now query it like this
            Cursor phones = myActivity.getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
            while (phones.moveToNext()) { 
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                int itype = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
    
                final boolean isMobile =
                    itype == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE ||
                    itype == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE;
    
                // Do something here with 'phoneNumber' such as saving into 
                // the List or Array that will be used in your 'ListView'.
    
            } 
            phones.close();
        }
    }
    

    【讨论】:

    • 很好的答案..谢谢哈马德,我真的为此苦苦挣扎了很长时间。我想知道为什么人们直到现在还没有投票!!
    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 2019-07-08
    • 2016-10-24
    • 2019-03-31
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多