【问题标题】:Get E-Mail, ID's and Phone from Contact (not using activity)从联系人获取电子邮件、ID 和电话(不使用活动)
【发布时间】:2011-06-14 16:48:20
【问题描述】:

我的班级没有 extends 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))这两个类?

【问题讨论】:

    标签: android javabeans android-context


    【解决方案1】:

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

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 2011-09-15
      • 2014-02-12
      相关资源
      最近更新 更多