2011-06-09 125 views
2

我還是很新的Android應用程序的開發,我已經打了一個問題,我希望你可以幫我...檢索聯繫人指出

我試圖檢索存儲反對任何「註釋」在我的手機內聯繫。我想檢查一個聯繫人(當前的呼叫者)是否有任何與他們有關的筆記,然後顯示內容或根據他們的內容做一些動作等等...

我試過下面的代碼作爲測試以查看數據是否在我的光標內的任何位置被檢索到,但雖然它檢索了一些數據,但我看不到註釋的內容 - 所以我想我錯了!

在我聲明瞭contentID的情況下,這是使用下面的代碼進行查找並使用接收到的id的結果。

Uri lookupUri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(inCommingNumber)); 
String[] mPhoneNumberProjection = { Phone._ID, Phone.NUMBER, Phone.DISPLAY_NAME}; 
Cursor cur = getContentResolver().query(lookupUri , mPhoneNumberProjection, null, null, null); 


private boolean hasNote(String contactID){ 
    Boolean noteFound = false; 

Cursor noteCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null, null, null); 
if(noteCur.moveToFirst()) { 
    int numCols = noteCur.getColumnCount(); 
    if(numCols > 0){ 
     for(int x = 0; x < numCols; x++){ 
      Log.d(APP_TAG, " column " + x + " contains: " + noteCur.getString(x)); 
     } 
    } 

    noteFound = true; 
} else{ 
    Log.d(APP_TAG, "No Note retrieved"); 
} 

noteCur.close(); 
return noteFound; 
} 

道歉,我似乎無法得到在這篇文章中正確顯示的代碼!

任何幫助將是巨大的

我曾嘗試過各種東西,但似乎無法能夠得到這些數據。我通過簡單地將其添加到常規聯繫人管理器中來創建該筆記。我正在使用Android 2.2。

回答

2

下面的代碼會顯示您的手機上的所有聯繫人的第一個音符:

Cursor contactsCursor = null; 
try { 
    contactsCursor = getContentResolver().query(RawContacts.CONTENT_URI, 
      new String [] { RawContacts._ID }, 
      null, null, null); 
    if (contactsCursor != null && contactsCursor.moveToFirst()) { 
     do { 
      String rawContactId = contactsCursor.getString(0); 
      Cursor noteCursor = null; 
      try { 
       noteCursor = getContentResolver().query(Data.CONTENT_URI, 
         new String[] {Data._ID, Note.NOTE}, 
         Data.RAW_CONTACT_ID + "=?" + " AND " 
           + Data.MIMETYPE + "='" + Note.CONTENT_ITEM_TYPE + "'", 
           new String[] {rawContactId}, null); 

       if (noteCursor != null && noteCursor.moveToFirst()) { 
        String note = noteCursor.getString(noteCursor.getColumnIndex(Note.NOTE)); 
        Log.d(APP_TAG, "Note: " + note); 
       } 
      } finally { 
       if (noteCursor != null) { 
        noteCursor.close(); 
       } 
      } 
     } while (contactsCursor.moveToNext()); 
    } 
} finally { 
    if (contactsCursor != null) { 
     contactsCursor.close(); 
    } 
} 

如果要存儲的筆記某種結構化數據的採取使用免費的形式提示欄上可能不會行動是最好的方法。通過新的聯繫人合同模型,您可以將自己的數據字段(在ContactsContract.Data中)附加到聯繫人,只需給他們自己獨特的mimetype即可。

+0

謝謝,我現在可以得到我需要的東西!我沒有存儲任何結構化的東西,只是檢查是否存在單詞或短語......我想盡可能地保持可用性(因此只使用標準音符)... 我只有一個問題你的代碼..它檢索所有聯繫人並檢查每個註釋。有沒有一種方法可以嘗試並僅檢索給定電話號碼的特定聯繫人? 我試着用下面的方法修改你的URI,但是這似乎沒有返回任何東西! 'Uri lookupUri = Uri.withAppendedPath(RawContacts.CONTENT_URI,Uri.encode(inCommingNumber));' – FireEnigmaX 2011-06-10 07:38:32

+0

RawContacts提供程序不支持該查找,您將不得不使用PhoneLookup表(而不是http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html)。但請記住,這會返回一個聯繫人而非原始聯繫人(這是該筆記將附加到的內容)。您需要獲取某個賬戶(例如電話,谷歌,交易所)的特定原始聯繫人,遍歷所有聯繫人,或者如果您加入聯繫數據表(不是rawcontactid),則可以處理也許多個音符。 – 2011-06-10 07:54:42

+0

謝謝,我根據你的回答排序 – FireEnigmaX 2011-06-11 15:33:22