2010-08-16 101 views
1

我一直在嘗試開發一個應用程序,需要從SIM存儲器訪問聯繫人。這裏是我用過的代碼,但有運行時異常。Android:從SIM卡存儲器訪問聯繫人

{ 
    Uri simUri = Uri.parse("content://icc/adn"); 
    c=getContentResolver().query(simUri, null, null, null, null); 
    startManagingCursor(c);   
    getContacts(); 
} 

private void getContacts(){ 
    int i=0; 
    do { 
     // Get the field values 
     names[i++] = c.getString(0);   
    } while (c.moveToNext()); 

任何答案和解決方案,讚賞。除此之外,您可以告訴我訪問SIM卡聯繫人的確切URI嗎?在sim的聯繫人記憶表中有沒有colums的名字?

+0

您能否添加您得到的異常? – Janusz 2010-08-16 09:48:28

+0

異常類似於「源找不到」,異常正好在 c = getContentResolver()。query(simUri,null,null,null,null); 是sim卡uri正確,對於所有的手機都是一樣的.. ?? – sujith 2010-08-16 17:00:30

回答

0

您的內容uri for sim contacts看起來不錯。 我正在使用以下方法獲得聯繫詳細信息 -

private void allSIMContact() { 
    try { 
     String m_simPhonename = null; 
     String m_simphoneNo = null; 
     String m_id = null; 

     Cursor cursorSim = this.getContentResolver().query(simUri, 
       null, null, null, ContactsContract.Contacts.DISPLAY_NAME); 

     Log.i("PhoneContact", "total: " + cursorSim.getCount()); 

     while (cursorSim.moveToNext()) { 
      m_id = cursorSim.getString(cursorSim.getColumnIndex("_id")); 
      m_simPhonename = cursorSim.getString(cursorSim 
        .getColumnIndex("name")); 
      m_simphoneNo = cursorSim.getString(cursorSim 
        .getColumnIndex("number")); 

      m_simphoneNo.replaceAll("\\D", ""); 
      m_simphoneNo.replaceAll("&", ""); 
      m_simPhonename = m_simPhonename.replace("|", ""); 

      ListData contact= new ListData(Integer.parseInt(m_id), 
        m_simphoneNo, m_simPhonename, "", "", "", false); 
      arrayList.add(contact); 

     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 
+0

「content:// icc/adn」是針對1.6以上版本的Android版本的1.5「content:// sim/adn」。 同時保證權限- 2015-01-04 00:02:28