无法从电话簿android获取联系人

时间:2014-02-26 03:16:27

标签: android android-contacts

我正在创建一个应用程序来获取Android设备电话簿中的所有联系人。我将我的联系人列表从Gmail同步到Android设备,我看到电话簿中的所有联系人。但是在运行应用程序时,无法从电话簿中获取任何联系人。

这是我的代码 MainActivity.java

public class MainActivity extends Activity {
    private TextView outputText;
    TextView txtViewContactsInfor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        outputText =(TextView)findViewById(R.id.textView2);
        txtViewContactsInfor = (TextView) findViewById(R.id.txtViewContactsInfor);
        Import_contacts_from_address_book();
    }


    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("---------------------->"+cursor.getCount());
        if(cursor.getCount() >0){
            int aa = 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)));

                if(hasPhoneNumber > 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();
        }
        outputText.setText(output.toString());
    }

我在cursor.getCount中调试并查看“544 contact”。但 int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));始终为零。

我不知道为什么。

2 个答案:

答案 0 :(得分:0)

您在Android清单中添加了阅读联系人权限

  

android.permission.READ_CONTACTS

答案 1 :(得分:0)

您可能有很多没有号码的联系人,因为联系人存储在多个列中。

我简化并修改了您的代码,以计算包含和不包含数字的行,并让它在我的手机上运行:

private void readContacts() {
    Cursor c = getContentResolver()
            .query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    int foundWithNumber = 0;
    int foundWithOutNumber = 0;
    if (c.moveToFirst()) {
        int idHasNumber = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
        do {
            boolean hasNumber = c.getInt(idHasNumber) > 0;
            if (hasNumber) {
                ++foundWithNumber;
            } else {
                ++foundWithOutNumber;
            }
        } while (c.moveToNext());
    }
    Log.d(TAG, "foundWithNumber: " + foundWithNumber);
    Log.d(TAG, "foundWithOutNumber: " + foundWithOutNumber);
    c.close();
}

我手机上的输出是:

      MainActivity  D  foundWithNumber: 184
                    D  foundWithOutNumber: 303

所以,我想,你只是看错了行。 ;)