在android中访问SIM卡联系人

发布于 2024-11-19 18:57:53 字数 1998 浏览 3 评论 0原文

我需要访问手机上的前 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野火S)上测试它时,我得到了所有空值。 我还使用手机联系人 uri、ContactsContract.Contacts.CONTENT_URI 对其进行了测试,效果很好。 我如何访问 SIM 卡联系人?我必须使用另一个 uri 吗?

i need to access the first 10 sim card contacts on my phone. the code i used is the following:

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();

}

when i tested it on the phone, an htc wildfire S, i got all null values.
i also tested it with the phone contacts uri, ContactsContract.Contacts.CONTENT_URI, and it worked fine.
how can i access SIM contacts? is there another uri i must use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2024-11-26 18:57:53

您的代码与 Uri 完美配合:

Uri uri16 =ContactsContract.Contacts.CONTENT_URI;

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

Your code is working perfectly with the Uri:

Uri uri16 =ContactsContract.Contacts.CONTENT_URI;

Make sure you have added the android permissions in the Android Manifest, like for example:
android.permission.READ_CONTACTS (Uses Permission).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文