开发者

Insert StatusUpdates into a specified contact, but always insert to a random contact

sample contacts:

_ID DISPLAY_NAME PHONE

1 contact1 11111111

2 contact2 22222222

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("22222222"));
Cursor c = this.getContentResolver().query(uri, new String[] {Data._ID}, null, null, null);
long profileId = 0;
if (c.moveToFirst())
{
    profileId = c.getLong(0);
}
c.close();
c = null;

final ContentValues values = new ContentValues();

if (profileId > 0) {
    values.put(StatusUpdates.DATA_ID, profileId);
    values.put(StatusUpdates.STATUS, "HELLO WORLD!");
    values.put(StatusUpdates.PROTOCOL, Im.PROTOCOL_CUSTOM);
    values.put(StatusUpdates.CUSTOM_PROTOCOL, CUSTOM_IM_PROTOCOL);
    values.put(StatusUpdates.PRESENCE, 4); //
    values.put(StatusUpdates.STATUS_RES_PACKAGE, this.getPackageName());
    values.put(StatusUpdates.STATUS_LABEL, R.string.label);

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(StatusUpdates.CONTENT_URI)
            .withValues(values).build());

    try{
        this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    }
    catch(RemoteException e)
    {Log.e...}
    catch(OperationApplicationException e)
    {Log.e...}
}

I'm trying to insert status to the specified contact "contact2", but it doesn't work correctly, and always insert to "contact1".

Please help me, man开发者_高级运维y thanks.


from sample sync adapter example :

    public static long lookupRawContact(ContentResolver resolver, String userId) 
{
    long authorId = 0;
    final Cursor c =
            resolver.query(RawContacts.CONTENT_URI, UserIdQuery.PROJECTION,
                    UserIdQuery.SELECTION, new String[] {userId},
                    null);
    try {
        if (c.moveToFirst()) {
            authorId = c.getLong(UserIdQuery.COLUMN_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return authorId;
}

This will return the correct profile ID or 0 if not found

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜