9

का उपयोग करते समय एकाधिक संख्या वाले उपयोगकर्ता से एक संख्या का चयन करना मैं उपयोगकर्ता को संपर्क पिकर का उपयोग करके किसी संपर्क से फोन नंबर चुनने की अनुमति देने की कोशिश कर रहा हूं। हालांकि, अभी ऑनलाइन देखे गए सभी उदाहरण दिखाते हैं कि आप एक संपर्क का चयन कैसे कर सकते हैं, लेकिन मुझे उम्मीद है कि दूसरी स्क्रीन होने की उम्मीद है तो उस संपर्क में कई फोन नंबर हैं, ताकि आप निर्दिष्ट कर सकें कि आप कौन सा चयन करना चाहते हैं (जिस तरह से आप चुनना चाहते हैं जब आप कोई संपर्क चुनते हैं तो वह टेक्स्ट संदेश आपको ऐसा करने देता है)।संपर्क पिकर

मेरा प्रश्न है, क्या आपको सभी संख्याओं को इकट्ठा करना है और फिर उपयोगकर्ता को नंबर चुनने के लिए कहा है, या यह कार्यक्षमता पहले ही एंड्रॉइड में बनाई गई है? मुझे उम्मीद है कि मैं सिर्फ झंडा या कुछ भूल गया था।

उत्तर

5

मैं एक दूसरा संवाद बनाकर ऐसा करने में सक्षम था जो संपर्क से जुड़े सभी फोन नंबर दिखाता है। पहले, अपने कोड में यह कहीं फोन:)

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
startActivityForResult(intent, REQUEST_PICK_CONTACT); 
फिर onActivityResult में

(इस का उपयोग करता है, तो चयनित संपर्क से अधिक फ़ोन नंबर है तय करने के लिए, और एक संवाद यदि ऐसा है तो प्रदर्शित:

Uri result = data.getData(); 
Log.v(TAG, "Got a result: " + result.toString()); 

// get the contact id from the Uri 
String id = result.getLastPathSegment(); 

// query for phone numbers for the selected contact id 
c = getContentResolver().query(
    Phone.CONTENT_URI, null, 
    Phone.CONTACT_ID + "=?", 
    new String[]{id}, null); 

int phoneIdx = c.getColumnIndex(Phone.NUMBER); 
int phoneType = c.getColumnIndex(Phone.TYPE); 

if(c.getCount() > 1) { // contact has multiple phone numbers 
    final CharSequence[] numbers = new CharSequence[c.getCount()]; 
    int i=0; 
    if(c.moveToFirst()) { 
     while(!c.isAfterLast()) { // for each phone number, add it to the numbers array 
      String type = (String) Phone.getTypeLabel(this.getResources(), c.getInt(phoneType), ""); // insert a type string in front of the number 
      String number = type + ": " + c.getString(phoneIdx); 
      numbers[i++] = number; 
      c.moveToNext(); 
     } 
     // build and show a simple dialog that allows the user to select a number 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle(R.string.select_contact_phone_number_and_type); 
     builder.setItems(numbers, new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int item) { 
       String number = (String) numbers[item]; 
       int index = number.indexOf(":"); 
       number = number.substring(index + 2); 
       loadContactInfo(number); // do something with the selected number 
      } 
     }); 
     AlertDialog alert = builder.create(); 
     alert.setOwnerActivity(this); 
     alert.show(); 

    } else Log.w(TAG, "No results"); 
} else if(c.getCount() == 1) { 
    // contact has a single phone number, so there's no need to display a second dialog 
} 

मैं पता है कि यह एक पुराना सवाल है लेकिन मुझे आशा है कि इससे मदद मिलती है।

+0

मैं नाम कैसे प्राप्त कर सकता हूं ?? – dmSherazi

+0

इससे बहुत मदद मिली, धन्यवाद !! –

+0

ने भी मेरी मदद की, धन्यवाद दोस्त! –

9

वैकल्पिक रूप से, आप प्रारंभ में संपर्क पिकर में प्रत्येक संपर्क से जुड़े फोन नंबर प्रदर्शित कर सकते हैं और इस तरह से एक का चयन कर सकते हैं। लॉन्च संपर्क पिकर इस तरह से (ध्यान दें मेरे अन्य जवाब से अलग URI):

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); 
startActivityForResult(intent, REQUEST_PICK_CONTACT); 

फिर, onActivityResult (में):

Uri result = data.getData(); 
Log.v(TAG, "Got a result: " + result.toString()); 

// get the phone number id from the Uri 
String id = result.getLastPathSegment(); 

// query the phone numbers for the selected phone number id 
Cursor c = getContentResolver().query(
    Phone.CONTENT_URI, null, 
    Phone._ID + "=?", 
    new String[]{id}, null); 

int phoneIdx = c.getColumnIndex(Phone.NUMBER); 

if(c.getCount() == 1) { // contact has a single phone number 
    // get the only phone number 
    if(c.moveToFirst()) { 
     phone = c.getString(phoneIdx); 
     Log.v(TAG, "Got phone number: " + phone); 

     loadContactInfo(phone); // do something with the phone number 

    } else { 
     Log.w(TAG, "No results"); 
    } 
} 
+0

लागू करने के लिए आसान, कम कोड। +1 –

+2

इसके अलावा, यदि आप कुछ कोड जोड़ते हैं, तो आप डिस्प्ले नाम भी प्राप्त कर सकते हैं: 'int nameIdx = c.getColumnIndex (Phone.DISPLAY_NAME); // नीचे यह डालें जहां फोन आईडी बनाया गया है ' और 'contactName = c.getString (nameIdx); // नीचे दिए गए फोन = c.getString (phoneIdx); ' –

+0

@howettl क्या कोई यूआरआई पासिंग है जो फोन नंबरों के साथ ईमेल चुनने के लिए एक ही संवाद (उपर्युक्त यूआरआई के रूप में संपर्क अनुप्रयोगों के भीतर) दिखाएगा? – cgr

3

शायद ज़रुरत पड़े कोई फिर से इस पार ठोकर।

अन्य उत्तर करने के लिए एक अन्य विकल्प पुस्तकालय https://github.com/codinguser/android_contact_picker

पूर्ण प्रकटीकरण है: https://developer.android.com/training/contacts-provider/modify-data.html#InsertEdit

और: मैं इस पुस्तकालय

+0

मुझे ईमेल चुनने के लिए अपनी लाइब्रेरी का विस्तार करने का एक तरीका भी ढूंढना चाहिए, (1 में से 1) !! –

+0

मुझे लगता है कि – codinguser

+0

करने के लिए अपेक्षाकृत आसान होना चाहिए वास्तव में इतना आसान नहीं है। वैसे भी, मैं इस पुस्तकालय के साथ और अधिक समय नहीं खोऊंगा, क्योंकि यह मेरे लिए बिल्कुल काम नहीं करता है (मैं ग्रहण का उपयोग कर रहा हूं)। तो, मुझे कुछ और समाधान मिल गया है। –

0

यह आसान एंड्रॉयड डेवलपर्स पर समझाया है संदर्भ के लेखक हूँ सरल संलग्न कोड:

String phoneNumber = "+01 123 456 789"; 
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT); 
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); 
intent.putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber); 
if (intent.resolveActivity(getPackageManager()) != null) { 
startActivityForResult(intent, REQUEST_CODE_ADD_PHONE_CONTACT); 
} 

यदि आपको गतिविधि के परिणाम की आवश्यकता है, तो आपको गतिविधि पर REQUEST_CODE_ADD_PHONE_CONTACT वैरिएबल पर एक्टिविटी रिसेट इवेंट पर सुनना होगा।

संबंधित मुद्दे