Synchronize phone book with my app

2

Would someone please help me with what would be the synchronization of mobile contacts with my application? I manage to obtain them through a code, but it turns out that they are repeated, so I see this is given by whatsapp, etc. Because I try on another device that does not have whatsapp or other similar installed, these contacts do not repeat ... the code with what I do this is:

public void ObtenerDatos() {

    int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.READ_CONTACTS);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para realizar llamadas telefónicas.");
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso para realizar llamadas!");
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
            // Se tiene permiso

            String[] projeccion = new String[]{ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
            String selectionClause = ContactsContract.Data.MIMETYPE + "='" +
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND "
                    + ContactsContract.CommonDataKinds.Phone.NUMBER + " IS NOT NULL";
            String sortOrder = ContactsContract.Data.DISPLAY_NAME + " ASC";

            Cursor c = getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    projeccion,
                    selectionClause,
                    null,
                    sortOrder);

            txtnombre.setText("");

            while (c.moveToNext()) {
                txtnombre.append(" Nombre: " + c.getString(0) + " Número: " + c.getString(1) + "\n");
            }
            c.close();

        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE_ASK_PERMISSIONS);
            return;
        }
    } else {
        // No se necesita requerir permiso OS menos a 6.0.
    }
}

As I said it works well, only when there is whatsapp, telegram or other similar service are repeated ...

    
asked by Wilson Cajisaca 05.11.2018 в 22:49
source

2 answers

1

I understand what Wilson says, your code is perfect, you get the phone number records in a cursor:

  String[] projeccion = new String[]{ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
            String selectionClause = ContactsContract.Data.MIMETYPE + "='" +
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND "
                    + ContactsContract.CommonDataKinds.Phone.NUMBER + " IS NOT NULL";
            String sortOrder = ContactsContract.Data.DISPLAY_NAME + " ASC";

            Cursor c = getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    projeccion,
                    selectionClause,
                    null,
                    sortOrder);

but here the problem is that the record you have of one or several phones of the same contact is actually different, as an example the telephone number of this user:

Cajamarca +593 96 058 7465
Cajamarca +593960587465
Cajamarca 0960587465

they are actually "different".

One solution is to add the standard telephone number to the projection: ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER

like this:

String[] projeccion = new String[]{ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER};

when reading this number you can check if the normalized number is in the list to avoid adding it:

 ...
 ...

           String[] projeccion = new String[]{ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER};
            String selectionClause = ContactsContract.Data.MIMETYPE + "='" +
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND "
                    + ContactsContract.CommonDataKinds.Phone.NUMBER + " IS NOT NULL";
            String sortOrder = ContactsContract.Data.DISPLAY_NAME + " ASC";

            Cursor c = getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    projeccion,
                    selectionClause,
                    null,
                    sortOrder);

            txtnombre.setText("");

                HashSet<String> normalizedNumbersAlreadyFound = new HashSet<>();
                int indexOfNormalizedNumber = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER);
                while (c.moveToNext()) {
                    String normalizedNumber = c.getString(indexOfNormalizedNumber);
                    if (normalizedNumbersAlreadyFound.add(normalizedNumber)) {

                        //*No se encuentra numero, lo agrega.
                        txtnombre.append(" Nombre: " + c.getString(0) + " Número: " + c.getString(1) + "\n");


                    }
                }
                c.close();
   ...
   ...

This way you will avoid showing repeated phone numbers.

    
answered by 05.11.2018 / 23:09
source
0

Check the variable of if and while because it seems that in one you fill the value with " Nombre: " + c.getString(0) + " Número: " + c.getString(1) + "\n" giving apparently fit the size of the number, but then in txtnombre.setText(""); you close the spaces and it takes it as a different field since both meet the requirements of if and while

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
        // Se tiene permiso

        String[] projeccion = new String[]{ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
        String selectionClause = ContactsContract.Data.MIMETYPE + "='" +
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND "
                    + ContactsContract.CommonDataKinds.Phone.NUMBER + " IS NOT NULL";
        String sortOrder = ContactsContract.Data.DISPLAY_NAME + " ASC";

        Cursor c = getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    projeccion,
                    selectionClause,
                    null,
                    sortOrder);

        xtnombre.setText("");

        while (c.moveToNext()) {
            txtnombre.append(" Nombre: " + c.getString(0) + " Número: " + c.getString(1) + "\n");
        }
        c.close();

    } else {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE_ASK_PERMISSIONS);
            return;
    
answered by 05.11.2018 в 23:44