【问题标题】:How to select multiple contacts from the phone using checkboxes如何使用复选框从手机中选择多个联系人
【发布时间】:2013-03-15 06:55:13
【问题描述】:

我正在尝试以编程方式从手机中选择可用的联系人,并且我正在使用以下代码

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

但问题是如何使用联系人页面中的复选框一次选择多个联系人?

【问题讨论】:

标签: android android-contacts


【解决方案1】:

您必须以编程方式读取联系人并将它们显示在您的Activity 中的ListView 中。在ListView 项目中使用CheckBoxs 并允许选择多个项目。为ListView 找到一个简单的示例/教程,然后从那里开始。

创建自定义ListView 比使用Intent(Intent.ACTION_GET_CONTENT); 更好的原因有几个:

  1. 可能无法按照您的要求选择多个。
  2. 即使你找到了选择倍数的方法,它也会有所不同 每个操作系统版本和设备,并且可能无法在所有设备上运行。
  3. 如果任何设备上安装了多个应用程序,可以 处理ACTION_GET_CONTENT,然后选择器将呈现给 用户,他将不得不选择其中之一。用户的选择 可能不支持选择多个联系人。

这是一个读取系统联系人的示例:

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

【讨论】:

  • 下面的答案在没有自定义列表视图的情况下效果很好:) 见@Gdroid
  • 这似乎仅适用于具有特定联系人应用程序的三星设备,该应用程序可用于此特定意图。大多数设备都会抛出android.content.ActivityNotFoundException: No Activity found to handle Intent
【解决方案2】:
   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);
}

【讨论】:

  • 这似乎仅适用于具有特定联系人应用程序的三星设备,该应用程序可用于此特定意图。大多数设备都会抛出android.content.ActivityNotFoundException: No Activity found to handle Intent
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多