How to add contacts directly to a group without creating an account?
I have this contacts that needs to be imported directly to a particular group that i created in which AFAIK that adding of conta开发者_运维百科cts 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?
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:
- Create your new raw contact in the same account your group will be in
- Save the contact (and let it sync)
- Create Your group for that same account in the group table (if it doesn't exist)
- 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).
- 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.
精彩评论