我正在开发一个应用程序,我正在使用
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 {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0) {
ArrayList arrayListContactID = new ArrayList();
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.对于删除联系人,我需要检查存储在我的数据库中的Raw 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");
}
我把日志.所以更新模块被多次调用,当我也添加或删除时间时……
请指导我并建议我做些什么来减少上述任务的时间……