Android get contact id from thread id

前端 未结 1 869
盖世英雄少女心
盖世英雄少女心 2021-01-24 18:07

I\'m working on a simple sms app and I\'m using the code below to get the thread id when loading my threads list but I can\'t figure out how to get the contact id using the thre

相关标签:
1条回答
  • 2021-01-24 18:40

    My solution to recover all contacts:

        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
            int contactIdIdx = cursor.getColumnIndex(Phone._ID);
            int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
            int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
            int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
            cursor.moveToFirst();
            do {
                String idContact = cursor.getString(contactIdIdx);
                String name = cursor.getString(nameIdx);
                String phoneNumber = cursor.getString(phoneNumberIdx);
                //...
            } while (cursor.moveToNext());  
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    

    You need this permission in your manifest :

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    

    I hope I have helped you!

    0 讨论(0)
提交回复
热议问题