【发布时间】:2014-02-18 05:15:11
【问题描述】:
我正在尝试一次性获取所有联系人数据。 我需要 : * 名 * 姓 * 电话号码 * 版本 等等
为此,我正在对整个联系人进行查询,然后,对于每个联系人 ID,我都会获取相关数据。
问题在于它需要很多时间,大约 400 个联系人需要超过 10 秒。 (在三星 i9300 上运行 4.4.2)
我不需要向用户显示任何内容,我只是在后台将联系人与我的服务器同步,所以我没有任何 UI 问题... 只需遍历联系人,看看需要如何更新并将他添加到我的 ArrayList, 然后 POST 到服务器。
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 也没有多大帮助。
【问题讨论】:
-
不,没有帮助,我用它来检查哪些联系人应该与我的服务器联系人同步。我在后台的另一个线程上做...
-
可以分页吗?先同步一段联系人,然后再滚动同步……这样会减少时间
-
我没有显示任何内容,只是将所有联系人同步到后台服务器。
标签: android sqlite android-contacts contactscontract