【发布时间】:2019-03-05 05:56:33
【问题描述】:
在我的 android 应用程序中,我试图从我的电话簿中获取联系人列表,有 1000 个联系人应用程序需要花费大量时间来加载联系人,而且它在此处崩溃是我的代码 当联系人正在加载它没有响应的应用程序崩溃
private ArrayList<AllInOnetItem> getContacts() {
String phoneNo = "";
ArrayList<AllInOnetItem> contacts = new ArrayList<>();
ContentResolver cr = getActivity().getContentResolver();
//ContentResolver cResolver=context.getContextResolver();
ContentProviderClient mCProviderClient = cr.acquireContentProviderClient(ContactsContract.Contacts.CONTENT_URI);
Cursor cur = null;
try {
cur = mCProviderClient.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
} catch (RemoteException e) {
e.printStackTrace();
}
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
contacts.add(new AllInOnetItem(name, phoneNo));
}
sortlist(contacts);
if (contactAdapter != null) {
contactAdapter.notifyDataSetChanged();
}
}
if (cur != null) {
cur.close();
}
return contacts;
}
【问题讨论】:
-
请联系
background thread。关于崩溃,请发布您的崩溃日志 -
它只是说应用程序没有响应
-
崩溃时。检查你的 android studio logcat
-
联系人已加载,但需要很长时间
-
在
AsyncTask进行联系人抓取操作