1

I am working with an Android application which shows the list of contacts in the phone like Whats-app. I am successful with getting the contacts to the Application database. When I try to update my DB when a contact is newly added/edited/deleted using Content Observer, I mainly face two problems 1. When Calls ,

String contactName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

I am getting wrong name. eg:- I edited Samuel's Contact no but checking the above code getting David's name.

  1. when I try to get contact number by using the code,

     contactNumber = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    

    I am getting error

    E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 402 rows, 41 columns.
    
    03-01 17:16:56.546 3660-3660/com.xxxxxxxxx.xxxxx W/System.err: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    

    Please Anybody help me to find the solution. Here is my Code

    public class ContactObserver extends ContentObserver {
    Intent i;
    private static final String TAG = "ObserverReceiver";
    
    private Context context;
    BaseActivity mBaseActivity;
    
    public ContactObserver(Handler handler, BaseActivity context) {
    super(handler);
    this.mBaseActivity = context;
    this.context = context;
    }
    
    public ContactObserver() {
    super(null);
    }
    
    @Override
    public boolean deliverSelfNotifications() {
    return true;
    }
     @Override
    public void onChange(boolean selfChange, Uri uri) {
    super.onChange(selfChange, uri);
    if (!selfChange) {
        try {
    
            ContentResolver cr = context.getContentResolver();
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            Log.d(TAG, "Observer has been started..");
    
    
            // Contacts Cursor
            final Cursor cur = context.getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    
            // Check which contact is added or updated
            if (cur != null) {
                while (cur.moveToNext()) {
                    // Get contact added/updated timestamp but CONTACT_LAST_UPDATED_TIMESTAMP
                    // Get new/updated contact detail here
    
                    final String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    final String contactName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    String contactNumber = null;
                    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        try {
                            contactNumber = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        } catch (Exception e) {
    
                            e.printStackTrace();
                        }
                    }
    
                    String finalContactNumber = contactNumber;
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            List<Contacts> sContacts = ContactDatabase
                                    .getDatabase(context)
                                    .contactsDao()
                                    .getBySystemid(id);
                            Log.i("Contacts1+", new Gson().toJson(sContacts));
                            if (sContacts.size() > 0) {
    
                                for (Contacts mContacts : sContacts) {
                                    String phone = finalContactNumber;
                                    if (phone == null) {
                                        phone = mContacts.getPhone();
                                    }
                                    Log.i("Contacts1+", contactName + " : " + phone);
                                    if (phone != null && phone.length() > 0) {
                                        phone = phone.replace(" ", "");
                                    }
                                    while (String.valueOf(phone.charAt(0)).equals("0")) {
                                        String num = "";
                                        for (int i = 1; i < phone.length(); i++) {
                                            num = num + phone.charAt(i);
                                        }
                                        phone = num;
                                    }
                                    if (phone.length() == 10) {
                                        phone = getSharedPreference(AppConstants.SharedKey.COUNTRY_CODE) + phone;
                                    }
                                    if ((mContacts.getName() != contactName || mContacts.getPhone() != phone)) {
                                        mContacts.setName(contactName);
                                        if (!mContacts.getPhone().equalsIgnoreCase(phone)) {
                                            mContacts.setPhone(phone);
                                            mContacts.setIslisted("false");
                                        }
                                        if (mContacts.getPhone().equalsIgnoreCase(phone) || mContacts.getName().equalsIgnoreCase(contactName)) {
                                            ContactDatabase.getDatabase(context)
                                                    .contactsDao()
                                                    .update(mContacts);
                                            cur.close();
    
                                        }
                                    }
                                }
                            } else {
                                Contacts mContacts = new Contacts();
                                String contactNumber = null;
                                try {
                                    contactNumber = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    if (contactNumber != null && contactNumber.length() > 0) {
                                        contactNumber = contactNumber.replace(" ", "");
                                        contactNumber = contactNumber.replace("-", "");
                                    }
                                    while (String.valueOf(contactNumber.charAt(0)).equals("0")) {
                                        String num = "";
                                        for (int i = 1; i < contactNumber.length(); i++) {
                                            num = num + contactNumber.charAt(i);
                                        }
                                        contactNumber = num;
                                    }
                                    if (contactNumber.length() == 10) {
                                        contactNumber = getSharedPreference(AppConstants.SharedKey.COUNTRY_CODE) + contactNumber;
                                    }
                                    String contactName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                                    mContacts.setPhone(contactNumber);
                                    mContacts.setName(contactName);
                                    mContacts.setSystemid(id);
                                    mContacts.setIslisted("false");
                                    List<Contacts> cContacts = ContactDatabase
                                            .getDatabase(context)
                                            .contactsDao()
                                            .getbyPhone(contactNumber);
                                    if (cContacts.size() == 0) {
                                        ContactDatabase.getDatabase(context)
                                                .contactsDao()
                                                .insert(mContacts);
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
    
                            }
                            i = new Intent(context, ContactSynchService.class);
                            context.startService(i);
                            cur.close();
    
                        }
                    });
                    thread.start();
                }
    
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }
    
    
    
        public String getSharedPreference(String key) {
            SharedPreferences prefs = mBaseActivity.getSharedPreferences(AppConstants.SHARED_KEY, MODE_PRIVATE);
            return prefs.getString(key, "DEFAULT");
        }
    
    
    }
    

I tried these links, but none of them could give a solution.

ContentObserver for contact update manually

ContentObserver not working in android

Android: ContentObserver not working in android 4.3

Any help will be appreciated. Thanks in Advance

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Cecil Paul
  • 595
  • 6
  • 27

0 Answers0