-2

Ich versuche, Telefonnummer als String in Android zu bekommen, gelang es mir, Kontakt zu bekommen und von ihm die Telefonnummer, aber das Ergebnis in den Protokollen ist Daten1 und die Nummer ist 32821 Ich verstehe mein Problem nicht.Wie bekomme ich die Telefonnummer von Kontakt in Android

Hier ist mein Code:

public void getContact(View view){ 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // for ActivityCompat#requestPermissions for more details. 

     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 10); 
//   return; 
    } 

    Intent contactsIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
    this.pickContact = 1; 
    startActivityForResult(contactsIntent, this.pickContact); 
} 

@Override 
public void onActivityResult(int reqCode, int resultCode, Intent data){ 
    super.onActivityResult(reqCode, resultCode, data); 

    if(reqCode == this.pickContact){ 
     if (resultCode == Activity.RESULT_OK) { 
      Log.d("ContactsH", "ResOK"); 
      Uri contactData = data.getData(); 
      Cursor contact = getContentResolver().query(contactData, null, null, null, null); 

      if (contact.moveToFirst()) { 
//     String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       String phoneNumber = ContactsContract.CommonDataKinds.Phone.NUMBER; 
       // TODO Whatever you want to do with the selected contact name and phone number. 

       Log.d("ContactsH", "Calling to:"+phoneNumber); 
       contact.close(); 
       this.callByNumber(phoneNumber); 
      } 
     } 
    }else{ 
     Log.d("ContactsH", "Canceled"); 
    } 
} 

können Sie mir bitte helfen?

+4

Mögliche Duplikat von [Wie Kontakte Telefonnummer in Android bekommen] (https://stackoverflow.com/questions/11218845/how-to-get-contacts -phone-nummer-in-android) –

+0

Hat 'String name = contact.getString (contact.getColumnIndex (KontakteContract.Contacts.DISPLAY_NAME));' nicht geben Sie den Namen? Hast du nicht dasselbe für die Nummer versucht? –

+0

Es tut mir leid, aber das funktioniert nicht für mich, weil es die gleiche Nummer zeigt, egal wen ich wähle. –

Antwort

1

Vielen Dank für @Levon Petrosyan

Aber ich brauche nur den Teil von seinem link und kopieren Sie sie in meiner Funktion hinzuzufügen.

Dies ist der Arbeitscode:

@Override 
public void onActivityResult(int reqCode, int resultCode, Intent data){ 
    super.onActivityResult(reqCode, resultCode, data); 

    if(reqCode == this.pickContact){ 
     if (resultCode == Activity.RESULT_OK) { 
      Log.d("ContactsH", "ResOK"); 
      Uri contactData = data.getData(); 
      Cursor contact = getContentResolver().query(contactData, null, null, null, null); 

      if (contact.moveToFirst()) { 
       String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       // TODO Whatever you want to do with the selected contact's name. 

       ContentResolver cr = getContentResolver(); 
       Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
         "DISPLAY_NAME = '" + name + "'", null, null); 
       if (cursor.moveToFirst()) { 
        String contactId = 
          cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
        // 
        // Get all phone numbers. 
        // 
        Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
          ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); 
        while (phones.moveToNext()) { 
         String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 
         switch (type) { 
          case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 
           // do something with the Home number here... 
           break; 
          case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: 
           // do something with the Mobile number here... 
           Log.d("ContactsH", number); 
           this.callByNumber(number); 
           break; 
          case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: 
           // do something with the Work number here... 
           break; 
         } 
        } 
        phones.close(); 
       } 
       cursor.close(); 
      } 
     } 
    }else{ 
     Log.d("ContactsH", "Canceled"); 
    } 
} 
Verwandte Themen