AsyncQueryHandler没有执行startQuery()

时间:2014-09-07 17:58:56

标签: android android-contentprovider android-handler

我正在努力解决以下奇怪的错误,并且可以查看其来源是否在我的代码或Android-19代码中。

我尝试使用AsyncQueryHandler查询内容提供程序异步,但它的onQueryComplete()从未被调用过。当我深入调试android源代码时,我注意到AsyncQueryHandler的handleMessage()永远不会被调用。即便如此,StartQuery()也会创建新消息并发布它。

以下代码从工作线程运行。 我会感激任何帮助。

private static final String[] PROJECTION_NAMES = { StructuredName.GIVEN_NAME, StructuredName.MIDDLE_NAME, StructuredName.FAMILY_NAME };
private static final String WHERE_NAMES = ContactsContract.Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "' AND "
            + StructuredName.LOOKUP_KEY + "=?";


ContentResolver cr = GlobalContext.getAppContext().getContentResolver();
AsyncQueryHandler asyncQueryHandler = new AsyncQueryHandler(cr) {

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor c) {

            if (c == null)
                return;

            if (c.moveToFirst()) {
                do {
                    String firstName = c.getString(INDEX_GIVEN_NAME);
                    String middleName = c.getString(INDEX_MIDDLE_NAME);
                    String lastName = c.getString(INDEX_FAMILY_NAME);

                    if (!TextUtils.isEmpty(firstName) || !TextUtils.isEmpty(middleName) || !TextUtils.isEmpty(lastName)) {
                        if (TextUtils.isEmpty(firstName)) {
                            firstName = middleName;
                        } else if (!TextUtils.isEmpty(middleName)) {
                            firstName += " " + middleName;
                        }

                        contact.setFirstName(firstName);
                        contact.setLastName(lastName);
                        break;
                    }
                } while (c.moveToNext());
            }

            c.close();
        }
    };

    token++;

    asyncQueryHandler.startQuery(token, new Object(), ContactsContract.Data.CONTENT_URI, PROJECTION_NAMES, WHERE_NAMES,
            new String[] { contact.getPhoneBookId() }, null);

0 个答案:

没有答案