以编程方式检索android中的SIM卡联系人

时间:2015-04-23 04:15:41

标签: android contacts

在我的Android应用程序中,我想检索所有的SIM卡和手机联系人。如果我单独执行代码它工作正常但如果我结合它我只是得到电话联系。如何获得两个联系人? 我的SIM联系人代码是:

private void SIMContacts()
{
  try
    {
      String strPhonename = null; 
        String strphoneNo = null;

    Uri simUri = Uri.parse("content://icc/adn"); 
    Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);

    while (cursorSim.moveToNext()) 
    {      
        strPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
        strphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
        strphoneNo.replaceAll("\\D","");
        strphoneNo.replaceAll("&", "");
        strPhonename=strPhonename.replace("|","");

        Log.i("Contact: ", "name: "+strPhonename+" phone: "+strphoneNo);
    }        
}
catch(Exception e)
{
    e.printStackTrace();
}
}

1 个答案:

答案 0 :(得分:1)

尝试使用它并从a arrayList获取Number List

public void getNumber(ContentResolver cr) {

    public static ArrayList<String> aa = new ArrayList<String>();
    Cursor phones = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);

    // System.out.println("Phone" + phones.getCount());

    while (phones.moveToNext()) {
        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        // System.out.println(".................." + phoneNumber);
        aa.add(name);
        aa.add(phoneNumber);
    }
    phones.close();// close cursor



    // display contact numbers in the list
}