Chapter 13. Access ContentProviders with AsyncQueryHandler

AsyncQueryHandler is a utility class that specializes in handling CRUD (Create, Read, Update, and Delete) operations on a ContentProvider asynchronously. The operations are executed on a separate thread, and when the result is available, callbacks are invoked on the initiating thread. Most commonly, the class is used to offload the ContentProvider operations from the UI thread, which receives the result once the background task has finished.

This chapter covers:

  • ContentProvider basics and concurrent access
  • How to implement and use the AsyncQueryHandler
  • Understanding the background execution

Brief Introduction to ContentProvider

This section contains some basic information on content providers. For more details, see the official documentation. A ContentProvider is an abstraction of a data source that can be accessed uniformly within the application or from other applications running in separate processes. The ContentProvider exposes an interface where data can be read, added, changed, or deleted through a database-centric CRUD approach with four access methods, as the skeleton code for EatContentProvider—a custom provider—shows:

public class EatContentProvider extends ContentProvider {

    private final static String STRING_URI=
    "content://com.eat.provider/resource";
    public final static Uri CONTENT_URI= Uri.parse(STRING_URI);

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs

Get Efficient Android Threading now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.