Java 应用程序在尝试读取联系人时被挂起

Java 应用程序在尝试读取联系人时被挂起,java,android,android-contacts,Java,Android,Android Contacts,我正在开发一个应用程序,可以读取手机联系人并以列表的形式显示。我必须向列表中选定的联系人发送消息。但问题是,如果手机上有超过500个联系人,应用程序就会被挂起。我不明白问题出在哪里 我在互联网上找到了这段代码,并在我的应用程序中实现了它。联系人将显示出来,但需要花费大量时间。这是我的密码 ContentResolver cr = getActivity().getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.

我正在开发一个应用程序,可以读取手机联系人并以列表的形式显示。我必须向列表中选定的联系人发送消息。但问题是,如果手机上有超过500个联系人,应用程序就会被挂起。我不明白问题出在哪里

我在互联网上找到了这段代码,并在我的应用程序中实现了它。联系人将显示出来,但需要花费大量时间。这是我的密码

ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null,null,null);
if (cur.getCount() > 0) {
   while (cur.moveToNext()) {
      String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
      String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
      if (Integer.parseInt(cur.getString(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()) {
              int phoneType = pCur.getInt(pCur.getColumnIndex(
                  ContactsContract.CommonDataKinds.Phone.TYPE));
              String phoneNumber = pCur.getString(pCur.getColumnIndex(
                  ContactsContract.CommonDataKinds.Phone.NUMBER));
              switch (phoneType) {
                    case Phone.TYPE_MOBILE:
                        Log.e(name + "(mobile number)", phoneNumber);
                        break;
                    case Phone.TYPE_HOME:
                        Log.e(name + "(home number)", phoneNumber);
                        break;
                    case Phone.TYPE_WORK:
                        Log.e(name + "(work number)", phoneNumber);
                        break;
                    case Phone.TYPE_OTHER:
                        Log.e(name + "(other number)", phoneNumber);
                        break;                                  
                    default:
                        break;
              }
          } 
          pCur.close();
    }
} }
阅读通讯录-

private void fetchContacts() {
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    while (phones.moveToNext()) {
        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        if (name == null || name.equals(""))
            name = phoneNumber;
        if (Utils.notNull(phoneNumber)) {
            phoneNumber = Utils.checkAndWrapMobileNumber(getApplicationContext(), phoneNumber);
            allContacts.put(phoneNumber, name);
            contactList.add(phoneNumber);
        }
    }
    phones.close();
}
最好在AssyncTask中使用此代码,以便在后台线程中读取


希望它能帮助您:)

这是由于
UI
线程中的繁重任务阻塞了
UI
,请使用
AyncTask
实现此目的

你有什么问题?滞后,联系人不显示?请给我们更多的信息张贴您的代码以及