Implementing AsyncQueryHandler

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

  • Strange Behavior of Android VideoView - “Can't Play Video”
  • Android: Difference between positive, negative, and neutral button
  • Facebook URL Scheme does not work fb://publish
  • KeyCode_Enter to next edittext
  • Running code in main thread from another thread
  • IllegalStateException at MediaCodec.dequeInputBuffer / dequeOutputBuffer
  • 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?

    Related posts:

    How to add json to the body of an http post in java
    How to disable android map marker click auto center
    Handle touch event for items inside Recyclerview - android
    How to use Doxygen plugin in Android Studio?
    Android View and ViewGroup
    How to solve java.util.zip.ZipException duplicate entry: com/google/gson/annotations/Expose.class?
  • Observable/Observer Android
  • Execution failed for task ':app:packageDebug' - Failed to read zip file
  • Android developers fee without credit card?
  • Android app out of memory issues - tried everything and still at a loss
  • android MediaPlayer not playing mp3 file
  • color value drawable resource issue
  • 4 Solutions collect form web for “Implementing AsyncQueryHandler”

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

    It’s so convenient:

    AsyncQueryHandler handler = 
        new AsyncQueryHandler(mActivity.getContentResolver()) {};
    handler.startUpdate(-1, null, sessionUri, values, null, null);
    

    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.

     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);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.