2015-05-16 60 views

回答

1

使用本:

Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,null); 
if (cursor != null && cursor.getCount() > 0) 
{ 
    if (cursor != null && cursor.getCount() != 0) 
    { 
     while (cursor.moveToNext()) 
     { 
      String contact_id = cursor.getString(cursor.getColumnIndex(_ID)); 
      ContentResolver contentResolver = ctx.getContentResolver(); 
      boolean hasPhoneNumberFlag = false; 

      int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER))); 

      if (hasPhoneNumber > 0) 
      { 
       Cursor cur = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI, null, ContactsContract.RawContacts.CONTACT_ID + "=" + Integer.valueOf(contact_id), null, null); 

       // Query and loop for every phone number of the contact 
       Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null); 
       int count = 0; 
       if (phoneCursor != null && phoneCursor.getCount() != 0) 
       { 
        int c = phoneCursor.getCount(); 
        while (phoneCursor.moveToNext()) 
        { 
         String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)); 
        } 

       } 
       if (phoneCursor != null) 
       { 
        phoneCursor.close(); 
       } 

       // insert in database 
       ContentValues valuesFor_Contact = new ContentValues(); 
       valuesFor_Contact.put("phonenumber", phone); 
       myDataBase.insert(TableName_Contacts, null, valuesFor_Contact); 
      } 

     } 

    } 

    if (cursor != null) 
    { 
     cursor.close(); 
    } 
} 
相關問題