【问题标题】:Android: Insert/Update/Delete contactAndroid:插入/更新/删除联系人
【发布时间】:2014-12-28 12:58:14
【问题描述】:

我正在开发一个应用程序,在其中列出设备联系人,并对它们进行一些操作。我听取以下链接中描述的联系人更改:Link 1Link 2

我的代码如下:

public class ContactService extends Service {
private int mContactCount;
Cursor cursor = null;
private int contactStateCheckingFlag=0;
static ContentResolver mContentResolver = null;

public static final String AUTHORITY = "com.example.contacts";
public static final String ACCOUNT_TYPE = "com.example.myapplication.account";
public static final String ACCOUNT = "myapplication";    
Account mAccount;

Bundle settingsBundle;    
int i=0;

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

@Override
public void onCreate() {
    super.onCreate();// Get contact count at start of service
    mContactCount = getContactCount();

    this.getContentResolver().registerContentObserver(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, true, mObserver);
    Cursor curval =  getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, null, null, null);

    if (curval != null && curval.getCount() > 0) {
        curval.getCount();
    }
    curval.close();
}

private int getContactCount() {
    try {
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,null);
        if (cursor != null) {
            return cursor.getCount();
        } else {
            cursor.close();
            return 0;
        }
    } catch (Exception ignore) {
    } finally {
        cursor.close();
    }       
    return 0;
}

private ContentObserver mObserver = new ContentObserver(new Handler()) {        
    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);         
        new ContactServiceAsyncClass().execute();           
    }       
};

private class ContactServiceAsyncClass extends AsyncTask<Void, Void, Void> {
    ArrayList<Integer> arrayListContactID = new ArrayList<Integer>();
    @Override
    protected void onPreExecute() {
        super.onPreExecute();           
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Get the current count of contacts
        int currentCount = getContactCount();

        // Add New Contact
        if (currentCount > mContactCount){
            Cursor contactCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

            if (contactCursor.getCount()<=0) {
                Log.d("Contact Cursor count"," is zero");
            }else {
                Log.d("Contact Cursor count", " > 0");                  
                // Fetch all contact ID from cursor
                if(contactCursor.moveToFirst()){
                    do {    
                        int contactID = contactCursor.getInt(contactCursor.getColumnIndex(ContactsContract.Data._ID));
                        arrayListContactID.add(contactID);
                    } while (contactCursor.moveToNext());
                }                   
                // Sort the array list having all contact ID
                Collections.sort(arrayListContactID);
                Integer maxID=Collections.max(arrayListContactID);
                Log.d("maxID", ""+maxID);                   
                // Get details of new added contact from contact id
                String whereName = ContactsContract.Data._ID + " = ?";// Where condition
                String[] whereNameParams = new String[] { ""+maxID}; // Pass maxID
                Cursor cursorNewContact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, whereName, whereNameParams, null);

                if(cursorNewContact.getCount()<=0){
                }else {
                    if(cursorNewContact.moveToFirst()){
                        do{
                            // Fetch new added contact details
                        } while(cursorNewContact.moveToNext());
                    }
                }
                cursorNewContact.close();
            }   
            contactCursor.close();
        } else if(currentCount < mContactCount){
            // Delete Contact/
            // CONTACT DELETED.
        } else if(currentCount == mContactCount){
            // Update Contact1
        }               
        mContactCount = currentCount;
        return null;
    }   

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}   
}

我能够获取新添加的联系人。问题是如何删除和更新联系人?由于联系人更改广播没有指定更改的联系人的 id,如何知道删除和更新了哪个联系人?

请您提出宝贵的建议并详细指导我。

谢谢。

【问题讨论】:

    标签: android android-service android-contentprovider android-contacts android-syncadapter


    【解决方案1】:

    对于删除操作,

    1.首先将之前的联系人 ID 列表存储在本地数据库中。

    例如:您添加的联系人 ID 为 123,124,125

    现在我们假设您最后添加的联系人 (125) 已被删除。

    我们如何找到它?

    简单先获取旧联系人列表。并与当前的联系人列表进行比较。

    如果旧的联系人列表元素不在新列表中,则该联系人将从手机中删除。

    注意:如果删除操作完成,需要将联系人ID更新到数据库中。

    对于更新操作,

    1. 使用 VERSION 标志来指示您的联系人的任何更改。

    2.VERSION默认值为1,修改联系人后自动增加到2。

    3.因此您需要将旧版本值存储在本地数据库中。并比较版本值增加与否。如果增加 VERSION 值,您需要更新此联系人。

    参考官方链接,

    https://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html

    对于完整的项目,

    http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.0.4_r2.1/com/android/exchange/adapter/ContactsSyncAdapter.java?av=f

    【讨论】:

    • 感谢您的宝贵回复。我搜索删除并在联系人中发现了一个删除标志,该标志将已删除的联系人存储 30 天。但它有效率吗?你有任何更新吗???
    • 我只知道使用 VERSION & DIRTY 的更新标志。我不知道删除标志。
    • @stacktry 请你看看我的问题issue
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 2014-02-14
    • 1970-01-01
    相关资源
    最近更新 更多