我正在努力將所有聯繫人數據歸爲一個。 我需要: *名 *姓 *電話號碼 *版本 等獲取Android中的所有聯繫人數據 - 多個URI
對於我在整個接觸做一個查詢,然後,爲每個聯繫人的ID,我得到的有關數據。
問題是,它需要很多時間,約400秒的接觸時間超過10秒。我不需要向用戶顯示任何東西,我只是在後臺同步聯繫人與我的服務器,所以我沒有任何UI問題... ... - - - - - - - - - - - - - 只是遍歷聯繫人,看看需要更新並將其添加到我的ArrayList, 然後將其發佈到服務器。
public static void getAllContactsFromDevice() {
long startTime = System.currentTimeMillis();
Log.d(TAG, "startTime: " + startTime);
// Get all contacts by cursor
ContentResolver cr = getContext().getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cursor.moveToFirst() && cursor.getCount() > 0) {
while (cursor.isAfterLast() == false) {
// get contact id on the device DB.
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String contactNumber = null;
// Get all phone numbers.
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
// if the user has numbers
// get just the first one for now
if (phones.moveToNext())
contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// close the cursor to prevent leak
phones.close();
// if we dont have a phone number, continue to next contact.
if (contactNumber == null) {
cursor.moveToNext();
continue;
}
// get first and last name by contact id
String[] projection = new String[]{ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME};
String where = ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] whereParameters = new String[]{contactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
Cursor nameCur = cr.query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);
String given = null;
String family = null;
try {
if (nameCur.getCount() > 0) {
nameCur.moveToFirst();
given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
nameCur.close();
}
// if there is no name, continue to next contact.
else {
nameCur.close();
cursor.moveToNext();
continue;
}
} catch (Exception e) {
e.printStackTrace();
nameCur.close();
}
if (given == null || given == "null")
given = "";
if (family == null || family == "null")
family = "";
String[] mProjection = new String[]{ContactsContract.RawContacts.VERSION};
String[] mWhereParameters = new String[]{contactId};
String mWhere = ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ? ";
Cursor mCursor = cr.query(ContactsContract.RawContacts.CONTENT_URI, mProjection, mWhere, mWhereParameters, null);
int contactVersion = 0;
try {
if (mCursor.getCount() > 0) {
mCursor.moveToFirst();
contactVersion = mCursor.getInt(mCursor.getColumnIndex(ContactsContract.RawContacts.VERSION));
mCursor.close();
} else {
mCursor.close();
}
} catch (Exception e) {
e.printStackTrace();
mCursor.close();
}
cursor.moveToNext();
}
}
cursor.close();
long endTime = System.currentTimeMillis();
Log.d(TAG, "endTime: " + endTime);
Log.d(TAG, "total time: " + (endTime - startTime));
}
總時間:18280毫秒
我看着HERE developer.android.com但沒有發現任何有用的東西。
也this answer沒有什麼幫助。
檢查此鏈接http://stackoverflow.com/questions/11991743/fetching-contact-detail-take-a-lot-of-time-in-android – deniz
不,沒有幫助,我用它來檢查哪個聯繫人應該與我的服務器聯繫人同步。我在另一個線程在背景中做... – Shahar
分頁是可能的嗎?先同步一部分聯繫人然後再滾動同步下一步.....它會減少時間 – deniz