Domanda

iam adding a contact to the device using this code

long Contact_Id = 100;
ContentValues pCV =new ContentValues();
pCV.put(Contacts.People.NAME, "test");
pCV.put(ContactsContract.Contacts._ID, Contact_Id);
Uri newContactUri = insertContentValues(cResolver,
                    Contacts.People.CONTENT_URI, pCV);

i want to add this contact to a certain Account. iam using this code below

ContentResolver cResolver = context.getContentResolver();
cResolver.insert(uri, ContactsContract.RawContacts.CONTENT_URI,
                        getAccountType()); 

public ContentValues getAccountType() {
    ContentValues cv = new ContentValues();  
    cv.put(ContactsContract.RawContacts.ACCOUNT_TYPE,  "com.sonyericsson.localcontacts");
    cv.put(ContactsContract.RawContacts.ACCOUNT_NAME, "Phone contacts");
    return cv;
}

this code actually is adding a new contact to the "Phone contacts" Account. but i want to add the contact that i added above ("test") to be added to the "Phone contacts". how can i do so?

È stato utile?

Soluzione

After searching I have found that the best way to insert a Contact to the Local Phone contacts is to assign the ACCOUNT_TYPE, ACCOUNT_NAME to null take a look at this Link

Altri suggerimenti

You can try both the solution as per your choice/requirement.Both are working perfectly

To add contact open directly edit activity

    try {
        Intent addContactIntent = new Intent(Intent.ACTION_INSERT);
        addContactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        addContactIntent.putExtra(ContactsContract.Intents.Insert.PHONE, number);
        addContactIntent.putExtra("finishActivityOnSaveCompleted", true);
        context.startActivity(addContactIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }

option to create contact or add contact to existing contact

 try {
        Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
        i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
        //  i.putExtra(ContactsContract.Intents.Insert.NAME, "TESTTEST");
        i.putExtra(ContactsContract.Intents.Insert.PHONE, number);
        context.startActivity(i);
    } catch (Exception e) {
        e.printStackTrace();
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top