在android中如何知道联系人被删除/更新/添加以及新添加的联系人

时间:2016-11-03 10:48:43

标签: android

我正在使用内容观察者知道对设备的联系电话簿进行了更改但我没有完成确定的任务,例如联系已添加已删除或更新的内容以及更新/添加的联系人的价值是多少。

//在后台运行的服务,它始终运行并检查内容是否已更改

public class ContactChange extends Service {


    ContactObserver observer;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        observer = new ContactObserver(new Handler(),getApplicationContext());
        // TODO Auto-generated method stub
        getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, false, observer);

        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getContentResolver().unregisterContentObserver(observer);
    }
}



//Content observer where we get to know that changes has made to the contact phonebook

    public class ContactObserver extends ContentObserver {

    private Context mContext;
    DataBaseCurdOperation dataBaseCurdOperation;
    ApiInterface apiInterface;
    MyPrefs myPrefs;
    ArrayList<InviteList> inviteArrayList;

    public ContactObserver(Handler handler, Context context) {
        super(handler);
        this.mContext = context;
        dataBaseCurdOperation = new DataBaseCurdOperation(mContext);
        myPrefs = new MyPrefs(mContext);
        apiInterface = ServiceGenerator.createService(ApiInterface.class, Config.BASE_URL_1);
        inviteArrayList = new ArrayList<InviteList>();
    }

    @Override
    public void onChange(boolean selfChange) {
        this.onChange(selfChange, null);
    }




    @Override
    public void onChange(boolean selfChange, Uri uri) {
        Logger.LogError("URI", uri.toString());
        boolean hasContactPermission = (ContextCompat.checkSelfPermission(mContext,
                android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED);
        if (hasContactPermission) {
            SavingContactsActivity savingContactsActivity = new SavingContactsActivity(mContext);
            savingContactsActivity.execute();
            new InviteApiCall().execute();
        }
    }

1 个答案:

答案 0 :(得分:1)

采用这种方法,它给联系人是否添加或更新没有得到删除的解决方案,但肯定会发布删除的答案很快.... 之后我在数据库上工作了

public class ContactSyncObserver extends ContentObserver {

Context mContext;
DataBaseCurdOperation dataBaseCurdOperation;
MyPrefs myPrefs;


public ContactSyncObserver(Handler handler, Context mContext) {
    super(handler);
    this.mContext = mContext;
    dataBaseCurdOperation = new DataBaseCurdOperation(mContext);
    myPrefs = new MyPrefs(mContext);

}

@Override
public boolean deliverSelfNotifications() {
    return true;
}

@Override
public void onChange(boolean selfChange, Uri uri) {
    super.onChange(selfChange, uri);
    boolean hasContactPermission = (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED);
    if (hasContactPermission) {
        try {
            Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP + " Desc");
            if (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                Logger.LogError("contactId", myPrefs.getContactId());
                String name = cursor.getString(
                        cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String rawContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));
                String phoneNumber = null;
                String hasPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                if (Integer.parseInt(hasPhoneNumber) > 0) {
                    Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
                    while (phones.moveToNext()) {
                        phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Log.e("Number", phoneNumber);
                    }
                    phones.close();
                }
                if (phoneNumber != null) {
                    phoneNumber = phoneNumber.replaceAll(" ", "");
                }
                if (dataBaseCurdOperation.checkIsContactIdExist(id)) {
                    if (!myPrefs.getContactId().equals(id)) {
                        dataBaseCurdOperation.updateNewNumber(id, phoneNumber, name, "updated");
                        UtilHandler.TriggerRefresh();
                    } else {
                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                myPrefs.setContactId("0");
                            }
                        }, 3000);
                    }
                } else {
                    dataBaseCurdOperation.insertServerContact(id, name, phoneNumber, "inserted", "newNumber", "newName");
                    UtilHandler.TriggerRefresh();  // triggering my sync adapter here...
                }
                myPrefs.setContactId(id);
            }
        } catch (Exception e) {
            Logger.LogError("Contact Exception", "occured");
        }
    }
}

}