title: How can get data from address-book faster in Android?
body: <p>I create an application to get all contact in addressbook (phone book) of Android device like Viper application.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>private void Import_contacts_from_address_book() {
          // TODO Auto-generated method stub
          String phoneNumber = null;
          String email = null;
          Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
          String _ID = ContactsContract.Contacts._ID;
          String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
          String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
  
          Uri PHONECONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
          String PHONECONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
          String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
  
          Uri EMAILCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
          String EMAILCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
          String EMAIL = ContactsContract.CommonDataKinds.Email.DATA;
  
          StringBuffer output = new StringBuffer();
          ContentResolver contentResolver = getContentResolver();
          Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);
          System.out.println("----------------------&gt;"+cursor.getCount());
          if(cursor.getCount() &gt;0){
              countContact = cursor.getCount();
              while(cursor.moveToNext()){
                  String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
                  String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
                  int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));
  
  
                  //------------------- Catch phone number, neu phone &gt;0 thi get phone, ko thi lam chuyen khac
                  //if(hasPhoneNumber &gt; 0 ){
                      output.append("\nFirst Name: "+name);
                      // Query and loop for every phone number of the contact
                      Cursor phoneCursor = contentResolver.query(PHONECONTENT_URI, null, PHONECONTACT_ID + "=?", new String[]{contact_id}, null);
                      while(phoneCursor.moveToNext()){
                          phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                          output.append("\n Phone number: "+phoneNumber);
                      }
                      phoneCursor.close();
                      // Query and loop for every email of the contact
  
                      Cursor emailCurosr = contentResolver.query(EMAILCONTENT_URI, null, EMAILCONTACT_ID+"=?",new String[]{contact_id},null);
                      while(emailCurosr.moveToNext()){
                          email = emailCurosr.getString(emailCurosr.getColumnIndex(EMAIL));
                          output.append("\nEmail: "+email);
                      }
                      emailCurosr.close();
                  //}
                  output.append("\n");
              }
              cursor.close();
          }
          txtViewContactsInfor.setText("Contacts: "+String.valueOf(countContact));
          outputText.setText(output.toString());
      }
  </code></pre>
  
  <p>I try to import 1000 contact and it take at least 45 second.</p>
  
  <ul>
  <li><p>Is there any way to improve speed? </p></li>
  <li><p>And when I change some information in contact of addressbook (phone book) in Android device. I using onResume() to update new information for my application but It's reload all page so It take a long time. But I see on Viper, the data updating immediately. So, is there any I can update or sync data from my device to my application like Viper application or improve speed updating, reload page ?</p></li>
  </ul>
  
  <p>Any help would be much appreciated! Thanks!</p>