检索未从Android联系人列表中删除的电话号码

时间:2015-09-06 16:35:43

标签: android contacts android-contacts

我正在尝试从ContactsContract中检索未标记为要删除的电话号码。

@Override
public Loader onCreateLoader(int id, Bundle args) {
    return new CursorLoader(
            this,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[]{
                    ContactsContract.CommonDataKinds.Phone.NUMBER
            },
            ContactsContract.CommonDataKinds.Phone.DELETED + "==0",
            null,
            null);
}

但是,我收到以下错误:

java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:304)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)
 Caused by: android.database.sqlite.SQLiteException: no such column: deleted (code 1): , while compiling: SELECT DISTINCT data1 FROM view_data data LEFT OUTER JOIN (SELECT data_usage_stat.data_id as STAT_DATA_ID, SUM(data_usage_stat.times_used) as times_used, MAX(data_usage_stat.last_time_used) as last_time_used FROM data_usage_stat GROUP BY data_usage_stat.data_id) as data_usage_stat ON (STAT_DATA_ID=data._id) WHERE (1 AND mimetype_id=5) AND ((deleted==0))
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
        at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
        at android.content.ContentResolver.query(ContentResolver.java:478)
        at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
        at android.content.CursorLoader.loadInBackground(CursorLoader.java:42)
        at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
        at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
        at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
        at android.os.AsyncTask$2.call(AsyncTask.java:292)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

The documentation for ContactsContract.CommonDataKinds.Phone.CONTENT_URI声明数据记录将与关联的原始联系人和汇总联系人数据相结合。但是"删除"不在这里。为什么以及如何才能获得未删除的电话号码?

3 个答案:

答案 0 :(得分:1)

数据URI投影没有DELETED列,在任何情况下,您的查询都应该只检索未删除的联系人的电话号码。您是否能够获取已删除原始联系人的电话号码?

答案 1 :(得分:0)

我认为你正在对错误的表进行查询。尝试将ContactsContract.CommonDataKinds.Phone.CONTENT_URI更改为ContactsContract.Contacts.CONTENT_URI

答案 2 :(得分:0)

我遇到了同样的问题,最终寻找最快的解决方案来检索联系人。

public static List<ContactDTO> getPhone(Context context){
        List<ContactDTO> contactList = new ArrayList<ContactDTO>();
        ContentResolver cr = context.getContentResolver(); 
        String[] PROJECTION = new String[] { 
                ContactsContract.RawContacts._ID, 
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Photo.CONTACT_ID };

        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String filter = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + Phone.TYPE +"=" + Phone.TYPE_MOBILE;     
        String order = ContactsContract.Contacts.DISPLAY_NAME + " ASC";// LIMIT " + limit + " offset " + lastId + "";

        Cursor phoneCur = cr.query(uri, PROJECTION, filter, null, order);
        while(phoneCur.moveToNext()){
             ContactDTO dto = new ContactDTO();
             dto.setName("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
             dto.setMobileNo("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
             dto.setPhotoUrl("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)));
             dto.setContactId("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID)));
             contactList.add(dto);
         }
         phoneCur.close();
        return contactList;
}

其中ContactDTO是Simple POJO class。