android - 从联系人获取电子邮件、ID 和电话(不使用 Activity )

标签 android javabeans android-context

我的类没有扩展 Activity 或 onCreate() 方法。因此,从将 Activity 扩展到此类的类传递上下文参数:

public static void getContactNumbers(Context context) {
        String contactNumber = null;
        int contactNumberType = Phone.TYPE_MOBILE;
        String nameOfContact = null;

            ContentResolver cr = context.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(BaseColumns._ID));
                    nameOfContact = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    if (Integer
                            .parseInt(cur.getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        Cursor phones = cr
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?", new String[] { id },
                                        null);

                        while (phones.moveToNext()) {
                            contactNumber = phones.getString(phones
                                    .getColumnIndex(Phone.NUMBER));
                            contactNumberType = phones.getInt(phones
                                    .getColumnIndex(Phone.TYPE));
                            Log.i(TAG, "...Contact Name ...." + nameOfContact
                                    + "...contact Number..." + contactNumber);
                            ApplicationConstants.phoneContacts
                                    .add(new ContactNumberBean(nameOfContact,
                                            contactNumber, contactNumberType));
                        }
                        phones.close();
                    }

                }
            }// end of contact name cursor
            cur.close();

    }

如何实现ApplicationConstantsContactNumberBean(nameOfContact, contactNumber, contactNumberType))这两个类?

最佳答案

这是几乎相同问题的解决方案 - Answer

希望对您有所帮助:)

关于android - 从联系人获取电子邮件、ID 和电话(不使用 Activity ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6347084/

相关文章:

java - Springboot无法通过 Autowiring 注解初始化和注入(inject)第三方bean

java - 如何在外部 Java 监听器中访问 getSupportFragmentManager

android-activity - 显示来自 android.app.application 的对话框

java - Android-带有图标的选项卡,未找到类

android - 调试仪器测试

android - android 4.0如何获取子节点的所有值?

java - 将空值从 Java bean 转换为 JSON

android - 这可行,但它是 "right"方式吗?

java - 制作 Spring Bean 列表

android - 来自静态方法的 getResources(),没有上下文