Android - 将所有手机号码读取到列表中

发布于 2024-12-07 00:12:28 字数 1069 浏览 0 评论 0原文

有人能给我一个正确的例子,说明如何将手机上保存的所有移动号码加载到列表、数组或任何合适的内容中吗?我发现的所有示例要么已贬值,要么不起作用。很抱歉要这样的免费赠品,但我越来越绝望,我找不到任何东西!

这是我所拥有的,它不起作用。 Log.d 不会发生。

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + People.NAME + "'", null, null);
    if (cursor.moveToFirst()){
        Log.d("Number", "Cursor moved");
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        Cursor phones = cr.query(People.CONTENT_URI, new String[]{People.NAME, People.NUMBER}, null, null, People.NAME + " ASC");

        while (phones.moveToNext()) {
            String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
            int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
            switch (type) {
                case Phone.TYPE_MOBILE:
                    //Add to the list of numbers
                    Log.d("Number", number);
                    break;
            }
        }
    }

谢谢你!

Can someone give me a correct example on how to load all MOBILE numbers saved on the phone into a List, Array or whatever is appropriate? All the examples I have found are either depreciated or do not work. Sorry to ask for a freebie like this but I am getting desparate, I can't find anything!

Here's what I have, it doesn't work. The Log.d doesn't happen.

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + People.NAME + "'", null, null);
    if (cursor.moveToFirst()){
        Log.d("Number", "Cursor moved");
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        Cursor phones = cr.query(People.CONTENT_URI, new String[]{People.NAME, People.NUMBER}, null, null, People.NAME + " ASC");

        while (phones.moveToNext()) {
            String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
            int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
            switch (type) {
                case Phone.TYPE_MOBILE:
                    //Add to the list of numbers
                    Log.d("Number", number);
                    break;
            }
        }
    }

Thank you!

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

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

发布评论

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

评论(1

随遇而安 2024-12-14 00:12:28

乔尔,

Cursor phones = cr.query( ContactsContract.CommonDataKind.Phone.NUMBER, .... );

您需要与 ContactsContract.CommonDataKind.Phone.TYPE = 2 进行比较。

谢谢。

Joel,

Cursor phones = cr.query( ContactsContract.CommonDataKind.Phone.NUMBER, .... );

And you need to compare with ContactsContract.CommonDataKind.Phone.TYPE = 2.

Thank you.

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