how can i get Viber contacts (Name and number)?

Viewed 154

I am trying to get Viber contacts. Using this method i get the name of the contacts that use Viber but also other contacts that do not use Viber (the selection method is not working properly) .I cant get the Viber contacts number also. Any idea how can i resolve this issue?

Cursor cursor2 = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI
            ,null
            , ContactsContract.RawContacts.ACCOUNT_TYPE_AND_DATA_SET + "= ?"
            ,new String[] { "com.viber.voip" }
            , null);

    while (cursor2.moveToNext())
    {
        String name=cursor2.getString(cursor2.getColumnIndex(ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY));

        //Adding contact name into the ArrayList
        myViberContacts.add(name+"\n");
        //Giving the TextView the value of the arraylist
        emervib.setText(myViberContacts.toString());
1 Answers

I believe this code should work for you:

String MIMETYPE_VIBER_MESSAGE = "vnd.android.cursor.item/vnd.com.viber.voip.viber_number_message";
String selection = Data.MIMETYPE + "=?";
String[] selectionArgs = new String[] { MIMETYPE_VIBER_MESSAGE };
String[] projection = new String[] { Data.CONTACT_ID, Data.DATA1, Data.DISPLAY_NAME };

Cursor cur = getContentResolver().query(Data.CONTENT_URI, projection, selection, selectionArgs, null);

DatabaseUtils.dumpCursor(cur); // check logcat for the results
Related