14

I am trying to implement AsyncQueryHandler because I am experiencing the same exact problem in this link but I dont see any example or anything about implementing it.

I tried doing

AsyncQueryHandler handler = new AsyncQueryHandler(getContentResolver());

since that is what the constructor shows in the docs but I get an error saying cannot instantiate the type AsyncQueryHandler so how do I use it then?

1
  • Good place to start would be the AOSP source code. Commented Aug 14, 2012 at 22:41

4 Answers 4

17

It's so convenient:

AsyncQueryHandler handler = 
    new AsyncQueryHandler(mActivity.getContentResolver()) {};
handler.startUpdate(-1, null, sessionUri, values, null, null);
Sign up to request clarification or add additional context in comments.

1 Comment

I get this warning when I try to do it This Handler class should be static or leaks might occur
12

It comes very handy when you need to do CRUD operationa async without using a Loader, so by extending from asyncquery you get method such as startQuery, startInsert, startUpdate and startDelete an easy implementation could be:

public class CustomAsyncHandler extends AsyncQueryHandler {

        private WeakReference<AsyncQueryListener> mListener;

        public interface AsyncQueryListener {
            void onQueryComplete(int token, Object cookie, Cursor cursor);
        }

        public CustomAsyncHandler(ContentResolver cr, AsyncQueryListener listener) {
            super(cr);
            mListener = new WeakReference<AsyncQueryListener>(listener);
        }

        public CustomAsyncHandler(ContentResolver cr) {
            super(cr);
        }

        /**
         * Assign the given {@link AsyncQueryListener} to receive query events from
         * asynchronous calls. Will replace any existing listener.
         */
        public void setQueryListener(AsyncQueryListener listener) {
            mListener = new WeakReference<AsyncQueryListener>(listener);
        }

        /** {@inheritDoc} */
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            final AsyncQueryListener listener = mListener.get();
            if (listener != null) {
                listener.onQueryComplete(token, cookie, cursor);
            } else if (cursor != null) {
                cursor.close();
            }
        }

    }

As you can see there is an extra interface AsyncQueryListener created to be as a callback when query is completed, as this you can create AsyncUpdateListener, AsyncDeleteListener, AsyncInsertListener, and so on.

7 Comments

Why would you not want to use a Loader when doing CRUD operations?
Because loader are not meant for CRUD operations!
As AsyncQueryHandler already has its own onQueryComplete() method, why is there any need to use AsyncQueryListener?
The extra listener interface is because you need to propagate the event back to the instantiating class.
simple and elegant, 10x, but why using WeakReference? no potential memory abuse in this case..
|
11

AsyncQueryHandler is an abstract class, thus you cannot instantiate it directly. You need to subclass it.

Comments

0
 AsyncQueryHandler asyncQueryHandler = new AsyncQueryHandler(activity.getContentResolver()){

        @Override
        protected void onInsertComplete(int token, Object cookie, Uri uri) {
            super.onInsertComplete(token, cookie, uri);
            Log.d(TAG, "onInsertComplete  return uri: " + uri);
        }

 };

 asyncQueryHandler.startInsert(-1, null, mUri, values);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.