将StatusUpdates插入指定的联系人,但始终插入随机联系人

时间:2011-08-09 04:22:51

标签: android 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...}
}

我正在尝试将状态插入指定的联系人“contact2”,但它无法正常工作,并始终插入“contact1”。

请帮助我,非常感谢。

1 个答案:

答案 0 :(得分:0)

来自示例同步适配器示例:

    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;
}

这将返回正确的配置文件ID,如果未找到则返回0