联系人更新到数据库不与ContentObserver一起使用

时间:2019-03-01 12:29:12

标签: android contacts contentobserver

我正在使用一个Android应用程序,该应用程序显示手机中的联系人列表,例如Whats-app。我已成功将联系人获取到应用程序数据库。当我使用Content Observer新添加/编辑/删除联系人时尝试更新数据库时,我主要遇到两个问题 1.通话时,

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

我叫错名字了。例如:-我编辑了塞缪尔的联系号码,但检查了上面的代码以获取戴维的名字。

  1. 当我尝试使用代码获取联系电话时,

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

    我遇到错误

    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.
    

    请任何人帮助我找到解决方案。 这是我的代码

    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");
        }
    
    
    }
    

我尝试了这些链接,但是没有一个可以提供解决方案。

ContentObserver for contact update manually

ContentObserver not working in android

Android: ContentObserver not working in android 4.3

任何帮助将不胜感激。在此先感谢

0 个答案:

没有答案