4

我需要访问我手机上的前 10 个 sim 卡联系人。我使用的代码如下:

public class simcontacts extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Uri uri16 = Uri.parse("content://icc/adn/");

    String[]SimContactsName = new String[250];
    String[]SimContactsNumber = new String[250];
    int count = 0;
    ContentResolver cr = getApplicationContext().getContentResolver();
    Cursor cur = cr.query(uri16,
            null, null, null, null);


    if (cur.getCount() > 0) {
    while (cur.moveToNext()) {


    String id = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts._ID));

    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
    {   
         Cursor pCur = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                null, 
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                new String[]{id}, null);
                while (pCur.moveToNext()) 
                {
                String contact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                SimContactsName[count]=contact;
                SimContactsNumber[count]=number;
                count++;
                } 
                pCur.close();
        }
        }
}

    TelephonyManager telMng = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

    for (int i=0; i<10;i++)
    {
        Message=Message+SimContactsNumber[i]+"\n";
    }

    Toast.makeText(this, Message, Toast.LENGTH_LONG).show();

}

当我在手机上测试它时,一个 htc wildfire S,我得到了所有的空值。我还使用电话联系人 uri ContactsContract.Contacts.CONTENT_URI 对其进行了测试,并且效果很好。如何访问 SIM 卡联系人?我必须使用另一个uri吗?

4

1 回答 1

0

您的代码与以下完美配合Uri

Uri uri16 =ContactsContract.Contacts.CONTENT_URI;

确保您已在 Android Manifest 中添加了 android 权限,例如:( android.permission.READ_CONTACTS使用权限)。

于 2012-01-17T10:04:03.737 回答