2010-08-16 102 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