2

Ich möchte Contacts Content Provider so abfragen, dass, wenn ein Kontakt IM hat, deren Typ gleich "XYZ" ist.Android: Holen Sie sich alle Kontakte von IM

ich unten Art und Weise versucht, aber ich bin kein Ergebnis bekommen:

Uri uri1 = ContactsContract.Contacts.CONTENT_URI; 
    String[] projection1 = null; 
    String selection1 = null; 
    String[] selectionArgs1 = null; 
    String sortOrder1 = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"; 
    Cursor cursor1 = context.getContentResolver().query(uri1, projection1, selection1, selectionArgs1, sortOrder1); 
    if (cursor1 != null && cursor1.getCount() > 0) { 
     while (cursor1.moveToNext()) { 
      int contactId = Integer.parseInt(cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts._ID))); 

      Uri uri2 = ContactsContract.Data.CONTENT_URI; 
      String[] projection2 = null; 
      String selection2 = ContactsContract.CommonDataKinds.Im.PROTOCOL + " = ? AND " + ContactsContract.Contacts._ID + " = ? "; 
      String[] selectionArgs2 = new String[]{"XYZ", contactId + ""}; 
      String sortOrder2 = null; 
      Cursor cursor2 = context.getContentResolver().query(uri2, projection2, selection2, selectionArgs2, sortOrder2); 
      if (cursor2 != null && cursor2.getCount() > 0) { 
       while (cursor2.moveToNext()) { 
        Log.i(TAG, "Name: " + cursor2.getString(cursor2.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))); 

       } 
       DatabaseUtils.dumpCursor(cursor2); 
      } 
     } 
     cursor1.close(); 
    } 

ich kein Protokoll mit obigem Code bin immer.

PS: Ich verwende keine eingebauten Protokolle wie AIM, Windows Live, Yahoo oder Skype. Es ist mein benutzerdefiniertes Protokoll, sag es "XYZ".

Antwort

0

Dazu müssen Sie die ContactsContract.Data.CONTENT_URI abfragen und mit dem MIME-Typ als IM und dann das Label oder Typ-Feld (nicht sicher) hält, welche Art von IM wie Sie sagten "XYZ" und in der Spalte Wert werden Sie Erhalte den Wert wie einen Benutzernamen.

In dieser Tabelle befindet sich ein Fremdschlüssel, der mit der Rohkontakt-ID der Tabelle raw_contacts verknüpft ist.

UPDATE

Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(
       ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.MIMETYPE + "=?", new String[]{ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE}, null); 
     if(cursor!=null) { 
      cursor.moveToFirst(); 
      do { 
       String value = cursor 
         .getString(cursor 
           .getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)); 

       //Types are defined in CommonDataKinds.Im.* 
       int imppType = cursor 
         .getInt(cursor 
           .getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE)); 

       //Protocols are defined in CommonDataKinds.Im.* 
       int imppProtocol = cursor 
         .getInt(cursor 
           .getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL)); 
       //and in this protocol you can check your custom value 
      }while (cursor.moveToNext()); 
      cursor.close(); 
     } 

Dank

+0

Einige Schnipsel wäre hilfreich. –

+0

finden Sie Code-Snippet –

Verwandte Themen