2012-05-01 63 views
0

我正在Android上開發IM應用程序。目標是具有多帳戶和協議兼容性。 我正在尋找一種獲得Android上聯繫人最佳狀態的方法。 我有一個支持的帳戶類型列表。我有一個功能需要很長時間(contact_id),並且必須返回最佳的Account +最佳聯繫方式(例如[email protected])。Android聯繫人操作

我已經查看了文檔,並且我已經知道了SQL。但是在這一點上文檔有點雜亂,我不知道如何獲得向聯繫人發送消息的最佳方式。這裏是我想要做一些代碼:

public static Account best(Context context, long contact) 
{ 
    final ContentResolver resolver = context.getContentResolver(); 
    final Cursor rawContacts = resolver.query(RawContacts.CONTENT_URI, 
       new String[]{RawContacts._ID}, 
       RawContacts.CONTACT_ID + "=? AND " + registeredProtocolsSelection(), 
       new String[]{String.valueOf(contact)}, null); 
    while(rawContacts.moveToNext()) 
    { 
     final long rawContactId = rawContacts.getLong(0); 
     Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId); 
     Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY); 
     Cursor rawContact = resolver.query(entityUri, 
        new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1}, 
        null, null, null); 
    } 
    return null; 
} 

我可以選擇事關我的應用程序,但當時我不知道如何加入所有RawContacts,我不能得到一個明確的架構數據庫。我不知道如何從那裏訪問StatusUpdate。目標就像ORDER BY presence

有沒有簡單的方法來做到這一點?有沒有辦法通過類似ORM的操作來像普通對象一樣操縱聯繫人?或者我們是否有機制在Android上輕鬆創建和管理自己的IM協議?

+0

我在我的研究ORMLite中發現,可以極大地幫助我。有誰知道Android的聯繫人的SQLite數據庫的架構? 或者更好的人有沒有人知道一個聯繫人類與正確的註釋來獲取數據? – grea09

回答

0

隨着時間的流逝,我發現它。我們可以使用ContentProvider.query的最後一個參數(...,sort);首先爲給定聯繫人提供最好的RawContact Presence。對於那些誰仍然搜索這裏是代碼

final Cursor cursor = contentResolver.query 
(
    StatusUpdates.CONTENT_URI, 
    new String[] 
    { 
     StatusUpdates.PRESENCE, 
     StatusUpdates.STATUS_LABEL, 
     StatusUpdates.STATUS_RES_PACKAGE 
    }, 
    Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?", 
    new String[] 
    { 
     Long.toString(id), 
     CommonDataKinds.Im.CONTENT_ITEM_TYPE 
    }, 
    StatusUpdates.STATUS_TIMESTAMP + " DESC" // For last Or 
    StatusUpdates.PRESENCE + " DESC" // For presence 
); 
if(cursor!=null && cursor.moveToFirst()) 
{ 
    presence = Presence.get(cursor.getLong(0)); 
    status = Utils.Contact.status(cursor.getInt(1), cursor.getString(2)); 
} 
if(cursor!=null) 
{ 
    cursor.close(); 
} 

您也可以使用聯繫表的predifined領域具有自動最佳狀態對於一個給定的接觸。