2015-10-22 67 views
4

我試圖打開接觸的意圖,讓用戶選擇multipile接觸
意圖電話:聯繫意圖有多個選擇

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

通過這種方法,用戶只能選擇一個聯繫人..我如何讓他選擇很多聯繫人,然後獲得他選擇的所有號碼?

回答

3

有兩個是這樣做的!

  1. 集MAX挑極限意圖:

    公共靜態最終詮釋REQUEST_CODE_PICK_CONTACT = 1; public static final int MAX_PICK_CONTACT = 10;

    私人無效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 公共無效onActivityResult(INT requestCode,INT發送resultCode,意圖數據){

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

}

注意:它可能會在每個操作系統版本和設備上有所不同,並且可能不適用於所有操作系統。

OR

  • 就做了這種方式:

    讀取聯繫人編程,並在你的活動一個ListView顯示出來,然後在使用複選框ListView項目並允許選擇多個項目。

  • 例如,你如何可以讀取您的系統聯繫人:

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

    很好的答案..感謝哈馬德我真的很努力這個很長一段時間。我想知道爲什麼人們還沒有upvoted到現在! –