android list 取 平均值_Android获取所有联系人列表(姓名,电子邮件,电话)需要超过一分钟的约700个联系人...

使用以下代码为59个联系人我在模拟器上得到以下结果:

D  ╔══════ query execution stats ═══════

D  ║    got 59 contacts

D  ║    query took 0.012 s (12 ms)

D  ╚════════════════════════════════════

好吧,这是最好的时间,但平均值是25-35毫秒(对于59个联系人),在一些onClick回调中添加以下代码并多次运行以获得平均时间,在你的情况下你应该得到30 * 700/59 = ~ 300-400毫秒,而不是3秒,更不用说一分钟;)

它使用Uri设置为API级别18中添加的Contactables.CONTENT_URI,但在构建18个API前设备时可以使用ContactsContract.Data.CONTENT_URIList list = new LinkedList();LongSparseArray array = new LongSparseArray();long start = System.currentTimeMillis();String[] projection = {

ContactsContract.Data.MIMETYPE,

ContactsContract.Data.CONTACT_ID,

ContactsContract.Contacts.DISPLAY_NAME,

ContactsContract.CommonDataKinds.Contactables.DATA,

ContactsContract.CommonDataKinds.Contactables.TYPE,};String selection = ContactsContract.Data.MIMETYPE + " in (?, ?)";String[] selectionArgs = {

ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,

ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,};String sortOrder = ContactsContract.Contacts.SORT_KEY_ALTERNATIVE;Uri uri = ContactsContract.CommonDataKinds.Contactables.CONTENT_URI;// we could also use Uri uri = ContactsContract.Data.CONTENT_URI;// ok, let's work...Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);final int mimeTypeIdx = cursor.getColumnIndex(ContactsContract.Data.MIMETYPE);final int idIdx = cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID);final int nameIdx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);final int dataIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.DATA);final int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.TYPE);while (cursor.moveToNext()) {

long id = cursor.getLong(idIdx);

AddressBookContact addressBookContact = array.get(id);

if (addressBookContact == null) {

addressBookContact = new AddressBookContact(id, cursor.getString(nameIdx), getResources());

array.put(id, addressBookContact);

list.add(addressBookContact);

}

int type = cursor.getInt(typeIdx);

String data = cursor.getString(dataIdx);

String mimeType = cursor.getString(mimeTypeIdx);

if (mimeType.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {

// mimeType == ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE

addressBookContact.addEmail(type, data);

} else {

// mimeType == ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE

addressBookContact.addPhone(type, data);

}}long ms = System.currentTimeMillis() - start;cursor.close();// done!!! show the results...int i = 1;for (AddressBookContact addressBookContact : list) {

Log.d(TAG, "AddressBookContact #" + i++ + ": " + addressBookContact.toString(true));}final String cOn = "";final String cOff = "";Spanned l1 = Html.fromHtml("got " + cOn + array.size() + cOff + " contacts
");Spanned l2 = Html.fromHtml("query took " + cOn + ms / 1000f + cOff + " s (" + cOn + ms + cOff + " ms)");Log.d(TAG, "\n\n╔══════ query execution stats ═══════" );Log.d(TAG, "║    " + l1);Log.d(TAG, "║    " + l2);Log.d(TAG, "╚════════════════════════════════════" );SpannableStringBuilder msg = new SpannableStringBuilder().append(l1).append(l2);LinearLayout ll = new LinearLayout(this);ll.setOrientation(LinearLayout.VERTICAL);TextView tv = new TextView(this);tv.setTextSize(20);tv.setBackgroundColor(0xff000033);tv.setPadding(24, 8, 24, 24);tv.setText(msg);ll.addView(tv);ListView lv = new ListView(this);lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list));ll.addView(lv);new AlertDialog.Builder(this).setView(ll).setPositiveButton("close", null).create().show();

助手AddressBookContact类:class AddressBookContact {

private long id;

private Resources res;

private String name;

private LongSparseArray emails;

private LongSparseArray phones;

AddressBookContact(long id, String name, Resources res) {

this.id = id;

this.name = name;

this.res = res;

}

@Override

public String toString() {

return toString(false);

}

public String toString(boolean rich) {

SpannableStringBuilder builder = new SpannableStringBuilder();

if (rich) {

builder.append("id: ").append(Long.toString(id))

.append(", name: ").append("\u001b[1m").append(name).append("\u001b[0m");

} else {

builder.append(name);

}

if (phones != null) {

builder.append("\n\tphones: ");

for (int i = 0; i 

int type = (int) phones.keyAt(i);

builder.append(ContactsContract.CommonDataKinds.Phone.getTypeLabel(res, type, ""))

.append(": ")

.append(phones.valueAt(i));

if (i + 1 

builder.append(", ");

}

}

}

if (emails != null) {

builder.append("\n\temails: ");

for (int i = 0; i 

int type = (int) emails.keyAt(i);

builder.append(ContactsContract.CommonDataKinds.Email.getTypeLabel(res, type, ""))

.append(": ")

.append(emails.valueAt(i));

if (i + 1 

builder.append(", ");

}

}

}

return builder.toString();

}

public void addEmail(int type, String address) {

if (emails == null) {

emails = new LongSparseArray();

}

emails.put(type, address);

}

public void addPhone(int type, String number) {

if (phones == null) {

phones = new LongSparseArray();

}

phones.put(type, number);

}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值