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:
publicclassEatContentProviderextendsContentProvider{privatefinalstaticStringSTRING_URI="content://com.eat.provider/resource";publicfinalstaticUriCONTENT_URI=Uri.parse(STRING_URI);@OverridepublicCursorquery(Uriuri,String[]projection,Stringselection,String[]selectionArgs
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access