开发者

Android - read all mobile numbers into a List

开发者 https://www.devze.com 2023-04-08 16:13 出处:网络
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

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 + 开发者_StackOverflow社区"'", 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!


Joel,

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

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

Thank you.

0

精彩评论

暂无评论...
验证码 换一张
取 消