【发布时间】:2016-05-12 11:04:33
【问题描述】:
我正在开发一个应用程序,在该应用程序中我正在处理 Android 联系人但无法继续前进。在应用程序中,应用程序的需要是更新的联系人应该发送到服务器或删除的联系人应该发送到服务器进行同步。
我正在使用联系服务:
public class ContactService extends Service {
private int mContactCount;
Cursor cursor = null;
static ContentResolver mContentResolver = null;
// Content provider authority
public static final String AUTHORITY = "com.android.contacts";
// Account typek
public static final String ACCOUNT_TYPE = "com.example.myapp.account";
// Account
public static final String ACCOUNT = "myApp";
// Instance fields
Account mAccount;
Bundle settingsBundle;
@Override
public void onCreate() {
super.onCreate();
// Get contact count at start of service
mContactCount = getContactCount();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Get contact count at start of service
this.getContentResolver().registerContentObserver(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, true, mObserver);
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
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) {
this.onChange(selfChange, null);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
new changeInContact().execute();
}
};
public class changeInContact extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0) {
ArrayList<Integer> arrayListContactID = new ArrayList<Integer>();
int currentCount = getContactCount();
if (currentCount > mContactCount) {
// Contact Added
} else if (currentCount < mContactCount) {
// Delete Contact
} else if (currentCount == mContactCount) {
// Update Contact
}
mContactCount = currentCount;
return "";
}
@Override
protected void onPostExecute(String result) {
contactService = false;
} // End of post
}
}
我面临的问题如下:
答:在上面获取最近更新的联系人的代码中,我需要检查设备中每个联系人的版本 与我的数据库存储版本 联系人。大量联系需要花费大量时间。
B. 为了删除联系人,我需要检查 存储在我的数据库中的原始 ID 的 数据 是否存在于设备中或不是。如果不是,则删除该联系人。检查整个联系人也需要太多时间。
但是同样的事情联系人刷新是在几秒钟内完成的,比如 2 到 3 秒...
编辑: 在以下模块中的上述代码中:
if (currentCount > mContactCount) {
// Contact Added
Log.d("In","Add");
} else if (currentCount < mContactCount) {
// Delete Contact
Log.d("In","Delete");
} else if (currentCount == mContactCount) {
// Update Contact
Log.d("In","Update");
}
我放了日志。所以更新模块被调用了很多次,当我添加或删除那个时候也是......
请指导我并建议我如何减少上述任务的时间......
【问题讨论】:
标签: android android-contentprovider android-contacts android-syncadapter