tags:

views:

10

answers:

1

I have this contacts that needs to be imported directly to a particular group that i created in which AFAIK that adding of contacts in an Account ,in the api seems to be broken(can't add other fields) so i implemented my own Activity to add accounts.

how do i add them directly to a group?

A: 

If you're talking about android 2.x, then groups exist on a per account basis. You need to first add your contact to an account before you can add it to a group.

You can add it to the default "device" account instead of a synced account if you know the account type/name for that device. These values are different for every maker I've tried writing code for. You should be able to get a list of these values from the accountmanager class. Null for both values appears to work on the nexus one, but fails spectacularly on every other phone I've tried.

These general steps should work: 1. Create your new raw contact in the same account your group will be in 2. Save the contact (and let it sync) 3. Create Your group for that same account in the group table (if it doesn't exist) 4. Add your synced contact to that new group by creating an entry in the groupmembership table (this is the data table, using the groupmembership mimetype). 5. Let the group sync.

Caution: As of at least android 2.1, before you can add new contacts to a new group you must add an existing (synced) contact to it first. If you do not and add new, unsynced contacts to a newly created, empty group, these contacts will disappear from the phone on the next sync and will not appear on the server (you'll lose the data). I'm not sure if this is a bug or just a limitation of the current group implementation.

Marloke
how about adding an account without sync since we don't need it?
mikedroid
Most devices have a non-syncing account included that you can use for this purpose. Unfortunately every device seems to have a different account name and type so you'll have to find out by experimenting with the AccountManager to see what accounts appear in the getAccounts list. I know that both samsung and HTC use very different accounts. Nexus one appears to use null (which if used on other devices, causes a crash). You might also attempt to add your own account and account type to the account manager, but as I haven't done this myself I don't have specific details on it.
Marloke