java - 从本地地址检索联系人

标签 java android sdk

我正在从本地地址簿中获取所有联系人,连同他们的姓名和电话号码,但问题是它是我用来交换邮件的返回电子邮件地址以及 Skype ID,这些我不需要: )

public void fetchContacts() {
        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 Phone_CONTACT_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 DATA = ContactsContract.CommonDataKinds.Email.DATA;
        StringBuffer output = new StringBuffer();

        ContentResolver contentResolver = getContentResolver();

        Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);

        // Loop for every contact in the phone
        if (cursor.getCount() > 0) {
            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("\n Name:" + name);
                    // 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);
                    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
                }
                output.append("\n");
            }
            outputText.setText(output);
        }


 <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

最佳答案

您的问题与 Android 无关,而是 Java 问题。

在最大的 while 循环中,确保 hasPhoneNumber IF 中有一个 continue 关键字,或者只是移动 output.append( "\n") 进入 IF。

干杯。 :-)

关于java - 从本地地址检索联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25762325/

相关文章:

java - Azure SDK + Java 库 + Eclipse 插件 = 一个困惑的灵魂

python - 将值从屏幕管理器屏幕问题传递到字段

java - 如何android单元测试和模拟静态方法

ios - iOS SDK Home 的默认路径未出现在 Titanium Studio 中

java - 为什么 jpa2/eclipselink 不生成删除级联 SQL?

java - Activity 的单一实例

java - Android MVVM 设计模式示例

android - Android Studio编译错误

java - OpenJDK 和 Adoptium/AdoptOpenJDK 的区别

关于监听器的 Java 并发问题