2015-05-27 22 views
5
/** 
    * Account type id 
    */ 
    public static final String ACCOUNT_TYPE = "com.test.app"; 

    /** 
    * Account name 
    */ 
    public static final String ACCOUNT_NAME = "Test"; 

public static void addContact(Context context, User contact) { 
     ContentResolver resolver = context.getContentResolver(); 
     resolver.delete(RawContacts.CONTENT_URI, RawContacts.ACCOUNT_TYPE 
       + " = ?", new String[] { AccountConstants.ACCOUNT_TYPE }); 

     ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 

     ops.add(ContentProviderOperation 
       .newInsert(
         addCallerIsSyncAdapterParameter(
           RawContacts.CONTENT_URI, true)) 
       .withValue(RawContacts.ACCOUNT_NAME, 
         AccountConstants.ACCOUNT_NAME) 
       .withValue(RawContacts.ACCOUNT_TYPE, 
         AccountConstants.ACCOUNT_TYPE) 
       // .withValue(RawContacts.SOURCE_ID, 12345) 
       // .withValue(RawContacts.AGGREGATION_MODE, 
       // RawContacts.AGGREGATION_MODE_DISABLED) 
       .build()); 

     ops.add(ContentProviderOperation 
       .newInsert(
         addCallerIsSyncAdapterParameter(Settings.CONTENT_URI, 
           true)) 
       .withValue(RawContacts.ACCOUNT_NAME, 
         AccountConstants.ACCOUNT_NAME) 
       .withValue(RawContacts.ACCOUNT_TYPE, 
         AccountConstants.ACCOUNT_TYPE) 
       .withValue(Settings.UNGROUPED_VISIBLE, 1).build()); 

     ops.add(ContentProviderOperation 
       .newInsert(
         addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true)) 
       .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
       .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) 
       .withValue(StructuredName.GIVEN_NAME, contact.getFullname()) 
       .withValue(StructuredName.FAMILY_NAME, contact.getFullname()) 
       .build()); 

     ops.add(ContentProviderOperation 
       .newInsert(
         addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true)) 
       .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
       .withValue(
         ContactsContract.Data.MIMETYPE, 
         ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, 
         contact.getPhoneNumber()).build()); 

     ops.add(ContentProviderOperation 
       .newInsert(
         addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true)) 
       .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
       .withValue(
         ContactsContract.Data.MIMETYPE, 
         ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.Email.DATA, 
         contact.getEmail()).build()); 

     ops.add(ContentProviderOperation 
       .newInsert(
         addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true)) 
       .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
       .withValue(Data.MIMETYPE, MIMETYPE) 
       .withValue(Data.DATA1, contact.getFullname()) 
       .withValue(Data.DATA2, contact.getEmail()) 
       .withValue(Data.DATA3, contact.getHomeAddress()).build()); 
     try { 
      ContentProviderResult[] results = resolver.applyBatch(
        ContactsContract.AUTHORITY, ops); 
      if (results.length == 0) 
       AppLog.d(TAG, "Failed to add."); 
     } catch (Exception e) { 
      AppLog.e(TAG, e.getMessage(), e); 
     } 
    } 

I would like to show my app account same as whats appWie individuelle Kontakt innen Kontakt wie WhatsApp android

Problem zeigen - Zur Zeit der Code neuen Kontakt hinzufügt, aber nicht in bestehende Kontakt basierend auf Telefonnummer zu verschmelzen. Muss ich etwas tun, bevor ich den Kontakt hinzufüge? Ich möchte meinen App-Account innerhalb von Whats App anzeigen.

Ich habe SyncService, SyncAdapter, Authenticator, contacts.xml und andere für das Projekt erforderliche Klassen implementiert. Die einzige Sache, die nicht funktioniert, zeigt Kontakt innerhalb der Standardkontakt App, anstatt neuen Kontakt herzustellen.

<ContactsSource xmlns:android="http://schemas.android.com/apk/res/android" > 

    <ContactsDataKind 
     android:detailColumn="data2" 
     android:detailSocialSummary="true" 
     android:icon="@drawable/ic_launcher" 
     android:mimeType="vnd.android.cursor.item/com.test.app" 
     android:summaryColumn="data3" /> 

</ContactsSource> 
+0

http://developer.android.com/guide/topics/providers/contacts-provider.html Abschnitt ** "Benutzerdefinierte Datenzeilen" ** – pskink

+0

Konnte dies erreicht werden? Ich suche nach der gleichen Lösung! – MohanRaj

+0

@Scorpion Ich bin auf der Suche nach der gleichen Lösung, können Sie mir helfen? – Spynet

Antwort

0

hatte ich das gleiche Problem auf Android 6.1 und wie ich gehört habe, ist dieses Thema seit Lollipop vorhanden. Jede Implementierung im Web zeigt, dass die Kontaktanpassung basierend auf der Telefonnummer funktionieren sollte. Und es funktioniert auf früheren Systemen - ich habe es auf KitKat versucht und es funktioniert wie ein Zauber. Aber da Android 5.0 Kontakte nicht irgendwie zusammenpassen.

Glücklicherweise ist die Telefonnummer nicht der einzige Parameter, von dem die Übereinstimmung abhängt - es gibt auch Anzeigename. So ist die Arbeits Umsetzung sollte wie folgt aussehen:

// as displayName pass the retrieved ContactsContract.Contacts.DISPLAY_NAME 

@Override 
public void addContact(@Nonnull final String displayName, @Nonnull final String phone) { 

    final ContentResolver resolver = context.getContentResolver(); 

     final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 

     ops.add(ContentProviderOperation 
       .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI, true)) 
       .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ACCOUNT_NAME) 
       .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE) 
       .withValue(ContactsContract.RawContacts.AGGREGATION_MODE, 
         ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT) 
       .build()); 

     ops.add(ContentProviderOperation 
       .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true)) 
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone) 
       .build()); 

     ops.add(ContentProviderOperation 
        .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true)) 
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 
        .withValue(ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName) 
        .build()); 

     ops.add(ContentProviderOperation 
       .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true)) 
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 
       .withValue(ContactsContract.Data.MIMETYPE, YOUR_MIMETYPE) 
       .withValue(ContactsContract.Data.DATA1, 12345) 
       .withValue(ContactsContract.Data.DATA2, "user") 
       .withValue(ContactsContract.Data.DATA3, "action") 
       .build()); 

     try { 
      resolver.applyBatch(ContactsContract.AUTHORITY, ops); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

ich die phonenumber links, falls der Kontakt keine displayname hat - dann wäre es zumindest Spiel auf älteren Android-Versionen.

+0

dies ist für das Hinzufügen eines neuen Kontaktes ..... aber ich möchte bestehenden Kontakt mit meinem neuen Mimetype (Symbol) zu aktualisieren .... wie kann ich das erreichen ??? jede Hilfe ... Danke ..... – Ramaraju

+0

Es ist tatsächlich ein Konto mit Ihrem Mimetype hinzufügen. Es würde genau wie das whatsapp-Beispiel angezeigt werden - als ein anderer Kontotyp, aber innerhalb desselben Kontakts – antrd