Search code examples
androidandroid-contactstelegramandroid-contact-mimetype

get all Telegram contacts saved in phone


I'm trying to get all Telegram contacts to use in my application ,here is my code :

        Cursor c = getContentResolver().query(
            ContactsContract.RawContacts.CONTENT_URI,
            new String[] { ContactsContract.RawContacts.CONTACT_ID, ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY },
            ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?",
            new String[] { "org.telegram.messenger.account" },
            null);

    ArrayList<String> myTelegramappContacts = new ArrayList<String>();
    int contactNameColumn = c.getColumnIndex(ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY);
    while (c.moveToNext())
    {
        // You can also read RawContacts.CONTACT_ID to read the
        // ContactsContract.Contacts table or any of the other related ones.
        myTelegramappContacts.add(c.getString(contactNameColumn));
    }

but it doesn't return me any contact!

what's my problem?

is org.telegram.messenger.account true for account type?or where can I find what is account type for telegram ?

Thanks for your attention.


Solution

  • finally found ,the true Account_Type for telegram is

    org.telegram.messenger
    

    and also ,can find all account type in a phone by this code:

        final Map<String, String> result = new HashMap<>();
        final Cursor cursor = getContentResolver()
                .query(ContactsContract.RawContacts.CONTENT_URI, new String[]{ContactsContract.RawContacts.ACCOUNT_TYPE, ContactsContract.RawContacts.ACCOUNT_NAME},
                        null, null,
                        null);
        final int accountNameIdx = cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME);
        final int accountTypeIdx = cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            result.put(cursor.getString(accountTypeIdx), cursor.getString(accountNameIdx));
        }
        cursor.close();
        Log.d("AppLog", "accounts found:");
        for (Map.Entry<String, String> account : result.entrySet())
            Log.d("AppLog", account.getKey() + ":" + account.getValue());
    

    hope to help anybody els.