的Android API 8,10 ContactsContract.Data.HAS_PHONE_

2019-07-30 19:13发布

下面的查询运行我的设备(API 15)的罚款。 它不会在我的模拟器上工作运行API 8或API 10。

下面是该查询:

Cursor contactsCur = getContentResolver().query(
  ContactsContract.Data.CONTENT_URI,
  new String[] { ContactsContract.Data._ID,
    ContactsContract.Data.CONTACT_ID,
    ContactsContract.Data.DISPLAY_NAME,
    ContactsContract.CommonDataKinds.Organization.COMPANY,
    ContactsContract.CommonDataKinds.Phone.TYPE,
    ContactsContract.Data.LOOKUP_KEY,
    ContactsContract.CommonDataKinds.Phone.NUMBER,
    Contacts.Data.MIMETYPE,
    ContactsContract.CommonDataKinds.Phone.MIMETYPE,
    ContactsContract.CommonDataKinds.StructuredPostal.CITY,
    ContactsContract.CommonDataKinds.StructuredPostal.STREET,
    ContactsContract.CommonDataKinds.StructuredPostal.REGION,
    ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE},
  ContactsContract.Data.HAS_PHONE_NUMBER + " >? ",
  new String[] { "0" },
  ContactsContract.Data.DISPLAY_NAME + " COLLATE NOCASE ASC");

下面是它抛出异常: android.database.sqlite.SQLiteException: no such column: has_phone_number: , while compiling: SELECT _id, contact_id, display_name, data1, data2, lookup, data1, mimetype, mimetype, data7, data4, data8, data9 FROM view_data_restricted data WHERE (1) AND (has_phone_number >? ) ORDER BY display_name COLLATE NOCASE ASC

奇怪的是,下面的代码也不起作用:

Cursor contactsCur = getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
    new String[] { ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER},
    ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + " >? ",
    new String[] { "0" },
    null);

它产生: java.lang.IllegalArgumentException: Invalid column has_phone_number

http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html指出ContactsContract.CommonDataKinds.Phone访问HAS_PHONE_NUMBER从接口android.provider.ContactsContract.ContactsColumns

为了增加陌生感,这个查询的工作:

Cursor contactsCur = getContentResolver().query(
    ContactsContract.Data.CONTENT_URI,
    new String[] { ContactsContract.Data.LOOKUP_KEY},
    null,
    null,
    null);

但是这一次没有:

Cursor contactsCur = getContentResolver().query(
    ContactsContract.Data.CONTENT_URI,
    new String[] { ContactsContract.Data.HAS_PHONE_NUMBER},
    null,
    null,
    null);

ContactsContract.Data.LOOKUP_KEY来自同一个隐含的加入为ContactsContract.Data.HAS_PHONE_NUMBER

我要去哪里错了?

Answer 1:

您正在运行到的主要问题是, ContactsContract.CommonDataKinds.Phone.CONTENT_URI 自动过滤has_phone_number=1 。 所以,你可以保证一切出来的Phone.CONTENT_URI 确实有一个电话号码。 然后,您可以进一步筛选下来通过检查Phone.TYPE是一个Phone.TYPE_MOBILEPhone.TYPE_WORK_MOBILE (举例来说),如果你想限制它有什么样的电话号码。


作为参考(注意has_phone_number是从缺席data/phones URI):

Columns for content://com.android.contacts/contacts:
- times_contacted
- contact_status
- custom_ringtone
- has_phone_number
- phonetic_name
- phonetic_name_style
- contact_status_label
- lookup
- contact_status_icon
- last_time_contacted
- display_name
- sort_key_alt
- in_visible_group
- _id
- starred
- sort_key
- display_name_alt
- contact_presence
- display_name_source
- contact_status_res_package
- contact_status_ts
- photo_id
- send_to_voicemail

和:

Columns for content://com.android.contacts/data/phones:
- data_version
- phonetic_name
- phonetic_name_style
- contact_id
- lookup
- data12
- data11
- data10
- mimetype
- data15
- data14
- data13
- display_name_source
- data_sync1
- data_sync3
- data_sync2
- data_sync4
- account_type
- custom_ringtone
- status
- data1
- data4
- data5
- data2
- data3
- data8
- data9
- group_sourceid
- data6
- account_name
- data7
- display_name
- in_visible_group
- display_name_alt
- contact_status_res_package
- is_primary
- contact_status_ts
- raw_contact_id
- times_contacted
- contact_status
- status_res_package
- status_icon
- contact_status_icon
- mode
- version
- last_time_contacted
- res_package
- _id
- name_verified
- status_ts
- dirty
- is_super_primary
- photo_id
- send_to_voicemail
- name_raw_contact_id
- contact_status_label
- status_label
- sort_key_alt
- starred
- sort_key
- contact_presence
- sourceid


文章来源: Android API 8 , 10 ContactsContract.Data.HAS_PHONE_NUMBER no such column