android content providers: asynchronous access to content ...schmidt/cs282/pdfs/... · android...
TRANSCRIPT
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Asynchronous Access to Content Providers
Android Content Providers Douglas C Schmidt
2
Learning Objectives in this Part of the Module
bull Understand the motivation for accessing Content Providers asynchronously
Android Content Providers Douglas C Schmidt
3
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
4
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
bull Cons bull Doesnrsquot leverage inherent parallelism
in the system
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
5
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
bull Cons bull Doesnrsquot leverage inherent parallelism
in the system bull Blocks the caller when performing queries
on the UI Thread bull Blocking is problematic for lengthy
operations such as loading data
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
6
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
7
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
bull Cons Can be hard to program unless you understand asynchrony patterns bull eg Proactor amp Asynchronous Completion Token
Async Access to Content Providers
wwwdrevanderbiltedu~schmidtPDFproactorACTpdf
Android Content Providers Douglas C Schmidt
8
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
9
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor bull Implements Loader protocol to
query cursors amp perform the cursor query on a background thread to not block the Apprsquos UI
Async Access to Content Providers
developerandroidcomreferenceandroidcontentLoaderhtml
Android Content Providers Douglas C Schmidt
10
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
bull Use an AsyncQueryHandler to make async ContentResolver queries easier
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
2
Learning Objectives in this Part of the Module
bull Understand the motivation for accessing Content Providers asynchronously
Android Content Providers Douglas C Schmidt
3
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
4
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
bull Cons bull Doesnrsquot leverage inherent parallelism
in the system
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
5
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
bull Cons bull Doesnrsquot leverage inherent parallelism
in the system bull Blocks the caller when performing queries
on the UI Thread bull Blocking is problematic for lengthy
operations such as loading data
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
6
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
7
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
bull Cons Can be hard to program unless you understand asynchrony patterns bull eg Proactor amp Asynchronous Completion Token
Async Access to Content Providers
wwwdrevanderbiltedu~schmidtPDFproactorACTpdf
Android Content Providers Douglas C Schmidt
8
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
9
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor bull Implements Loader protocol to
query cursors amp perform the cursor query on a background thread to not block the Apprsquos UI
Async Access to Content Providers
developerandroidcomreferenceandroidcontentLoaderhtml
Android Content Providers Douglas C Schmidt
10
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
bull Use an AsyncQueryHandler to make async ContentResolver queries easier
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
3
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
4
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
bull Cons bull Doesnrsquot leverage inherent parallelism
in the system
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
5
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
bull Cons bull Doesnrsquot leverage inherent parallelism
in the system bull Blocks the caller when performing queries
on the UI Thread bull Blocking is problematic for lengthy
operations such as loading data
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
6
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
7
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
bull Cons Can be hard to program unless you understand asynchrony patterns bull eg Proactor amp Asynchronous Completion Token
Async Access to Content Providers
wwwdrevanderbiltedu~schmidtPDFproactorACTpdf
Android Content Providers Douglas C Schmidt
8
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
9
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor bull Implements Loader protocol to
query cursors amp perform the cursor query on a background thread to not block the Apprsquos UI
Async Access to Content Providers
developerandroidcomreferenceandroidcontentLoaderhtml
Android Content Providers Douglas C Schmidt
10
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
bull Use an AsyncQueryHandler to make async ContentResolver queries easier
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
4
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
bull Cons bull Doesnrsquot leverage inherent parallelism
in the system
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
5
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
bull Cons bull Doesnrsquot leverage inherent parallelism
in the system bull Blocks the caller when performing queries
on the UI Thread bull Blocking is problematic for lengthy
operations such as loading data
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
6
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
7
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
bull Cons Can be hard to program unless you understand asynchrony patterns bull eg Proactor amp Asynchronous Completion Token
Async Access to Content Providers
wwwdrevanderbiltedu~schmidtPDFproactorACTpdf
Android Content Providers Douglas C Schmidt
8
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
9
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor bull Implements Loader protocol to
query cursors amp perform the cursor query on a background thread to not block the Apprsquos UI
Async Access to Content Providers
developerandroidcomreferenceandroidcontentLoaderhtml
Android Content Providers Douglas C Schmidt
10
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
bull Use an AsyncQueryHandler to make async ContentResolver queries easier
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
5
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider bull Pros ldquoIntuitiverdquo since it maps nicely onto
conventional ldquorequestresponse method call interactions
bull Cons bull Doesnrsquot leverage inherent parallelism
in the system bull Blocks the caller when performing queries
on the UI Thread bull Blocking is problematic for lengthy
operations such as loading data
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
6
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
7
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
bull Cons Can be hard to program unless you understand asynchrony patterns bull eg Proactor amp Asynchronous Completion Token
Async Access to Content Providers
wwwdrevanderbiltedu~schmidtPDFproactorACTpdf
Android Content Providers Douglas C Schmidt
8
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
9
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor bull Implements Loader protocol to
query cursors amp perform the cursor query on a background thread to not block the Apprsquos UI
Async Access to Content Providers
developerandroidcomreferenceandroidcontentLoaderhtml
Android Content Providers Douglas C Schmidt
10
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
bull Use an AsyncQueryHandler to make async ContentResolver queries easier
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
6
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
7
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
bull Cons Can be hard to program unless you understand asynchrony patterns bull eg Proactor amp Asynchronous Completion Token
Async Access to Content Providers
wwwdrevanderbiltedu~schmidtPDFproactorACTpdf
Android Content Providers Douglas C Schmidt
8
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
9
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor bull Implements Loader protocol to
query cursors amp perform the cursor query on a background thread to not block the Apprsquos UI
Async Access to Content Providers
developerandroidcomreferenceandroidcontentLoaderhtml
Android Content Providers Douglas C Schmidt
10
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
bull Use an AsyncQueryHandler to make async ContentResolver queries easier
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
7
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations bull Pros Leverages inherent parallelism
more effectively amp doesnrsquot block the UI Thread
bull Cons Can be hard to program unless you understand asynchrony patterns bull eg Proactor amp Asynchronous Completion Token
Async Access to Content Providers
wwwdrevanderbiltedu~schmidtPDFproactorACTpdf
Android Content Providers Douglas C Schmidt
8
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
9
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor bull Implements Loader protocol to
query cursors amp perform the cursor query on a background thread to not block the Apprsquos UI
Async Access to Content Providers
developerandroidcomreferenceandroidcontentLoaderhtml
Android Content Providers Douglas C Schmidt
10
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
bull Use an AsyncQueryHandler to make async ContentResolver queries easier
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
8
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
9
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor bull Implements Loader protocol to
query cursors amp perform the cursor query on a background thread to not block the Apprsquos UI
Async Access to Content Providers
developerandroidcomreferenceandroidcontentLoaderhtml
Android Content Providers Douglas C Schmidt
10
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
bull Use an AsyncQueryHandler to make async ContentResolver queries easier
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
9
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor bull Implements Loader protocol to
query cursors amp perform the cursor query on a background thread to not block the Apprsquos UI
Async Access to Content Providers
developerandroidcomreferenceandroidcontentLoaderhtml
Android Content Providers Douglas C Schmidt
10
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
bull Use an AsyncQueryHandler to make async ContentResolver queries easier
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
10
bull All Activities thus far have invoked synchronous two-way calls to query ContentResolverProvider
bull An alternative on Android involves the use of two-way asynchronous operations
bull Asynchronous Android models include bull Use a CursorLoader to query the
ContentResolver amp return a Cursor
bull Use an AsyncQueryHandler to make async ContentResolver queries easier
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Async Access to Content Providers
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
11
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to ContentProviders
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
12
Summary bull Asynchrony is a powerful technique
that Android supports to optimize access to Content Providers
bull Asynchronous access to Content Providers is very common in Android Apps eg bull Browser bull Calendar bull Contacts bull Email bull MMSSMS
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with the LoaderManager
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
14
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the LoaderManager framework amp CursorLoaders
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
15
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
16
bull An abstract class that performs asynchronous loading of data bull Loaders ensure that all cursor
operations are done ldquoin the backgroundrdquo thereby eliminating the possibility of blocking the UI Thread
bull While Loaders are active they monitor the source of their data amp deliver new results when the contents change
developerandroidcomreferenceandroidcontentLoaderhtml
Overview of Loader
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
17
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it bull eg a LoaderManager is in charge of
starting stopping retaining restarting amp destroying its Loaders
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
18
bull Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it
bull When a Loader is managed by a LoaderManager it retains its existing cursor data across the Activity or Fragment instance bull eg when a restart occurs due to
a configuration change the cursor neednrsquot perform unnecessary potentially expensive re-queries
developerandroidcomreferenceandroidappLoaderManagerhtml
Overview of LoaderManager
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
19
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
20
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider bull It returns the result to the Activity or
Fragment from which it was called bull It does not block the Apps UI
bull The Activity or Fragment can thus continue to interact with the user which the query is ongoing
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
21
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) return new CursorLoader (context uri null null null null)
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
22
bull A CursorLoader runs an asynchronous query in the background against a ContentProvider
bull A CursorLoader can be built with the full info for the query to perform
bull You can also create empty instance with CursorLoader(Context) amp fill in desired parameters with bull setUri() bull setSelection() bull setSelectionArgs() bull setSortOrder() bull setProjection()
developerandroidcomreferenceandroidcontentCursorLoaderhtml
Overview of CursorLoader Create a new CursorLoader with the query parameters public CursorLoader makeCursorLoader (Context context Uri uri) CursorLoader cl = new CursorLoader (context) Set the desired URI clsetUri(uri)
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
23
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) return makeCursorLoader (getApplicationContext() ImageProviderIMAGES_URI)
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
24
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading bull LoaderManager can callback
to onLoadFinished() method each time ContentProviderrsquos data is updated
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) loadImageFromCursor(c)
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
25
bull To use the LoaderManager amp CursorLoader have your Activity implement the LoaderManager LoaderCallbacksltCursorgt class amp override the following methods bull onCreateLoader() ndash Hook
method that instantiates amp returns a new Loader
bull onLoadFinished() ndash Hook method called when a previously created loader has finished its loading
bull onLoaderReset() ndash Hook method called when a created loader is being reset making its data unavailable
developerandroidcomreferenceandroidappLoaderManagerLoaderCallbackshtml
Using LoaderManager amp CursorLoader public class DownloadActivity extends Activity implements LoaderCallbacks ltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle) public void onLoadFinished (LoaderltCursorgt a Cursor c) public void onLoaderReset (LoaderltCursorgt loader) onClickReset(null)
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
26
Example of LoaderManager ContentProvider bull Shows how to use LoaderManager amp
CursorLoader to implement a ContentProvider that is accessed asynchronously
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
27
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
28
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations bull All of which are implemented as
synchronized Java methods
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
29
Example of LoaderManager ContentProvider bull Shows how to implement a
ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
bull Supports all the ContentProvider ldquoCRUDrdquo operations
bull Client Activity accesses the ContentProvider using asynchronous two-way calls made via a LoaderManager amp CursorLoader
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
30
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt private static final int LOADER_ID = 0 private LoaderManagerLoaderCallbacksltCursorgt mCallbacks private SimpleCursorAdapter mAdapter
This Activity handles callbacks from the LoaderManager
The callbacks through which we interact with the LoaderManager
The adapter that binds our data to the ListView
The loaders unique id
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
31
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) String[] dataColumns = _id data int[] viewIDs = RididString Riddata mAdapter = new SimpleCursorAdapter(this Rlayoutlist_layout null dataColumns viewIDs 0)
ContactProviderActivityAsync Example
Do all the same initialization as before
Map columns from an initially null cursor to TextViews or ImageViews defined in an XML file
developerandroidcomreferenceandroidwidgetSimpleCursorAdapterhtml
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
32
public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onCreate(Bundle savedInstanceState) setListAdapter(mAdapter) mCallbacks = this getLoaderManager()initLoader(LOADER_ID null mCallbacks)
ContactProviderActivityAsync Example
Associate adapter wListView
Activity is the callback object for LoaderManager
Initialize Loader with id amp mCallbacks
developerandroidcomreferenceandroidappLoaderManagerhtml initLoader(int androidosBundle LoaderManagerLoaderCallbacksltDgt)
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
33
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public LoaderltCursorgt onCreateLoader(int id Bundle args) return new CursorLoader(ContentProviderActivityAsyncthis MyCPCONTENT_URI null null null null)
Create a new CursorLoader with query parameter
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
34
ContactProviderActivityAsync Example public class ContactProviderActivityAsync extends ListActivity implements LoaderManagerLoaderCallbacksltCursorgt public void onLoadFinished(LoaderltCursorgt loader Cursor cursor) switch (loadergetId()) case LOADER_ID mAdapterswapCursor(cursor)
Async load is complete amp data is available for SimpleCursorAdapter
The listview now displays the queried data
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
35
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
36
Summary bull The LoaderManager framework helps an App
manage longer-running operations in conjunction with the Activity or Fragment lifecycle
bull The most common use of LoaderManager is with a CursorLoader bull Apps can write their own loaders for
loading other types of data
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Douglas C Schmidt dschmidtvanderbiltedu
wwwdrevanderbiltedu~schmidt
Professor of Computer Science Institute for Software Integrated Systems
Vanderbilt University
Nashville Tennessee USA
Android Content Providers Programming with AsyncQueryHandler
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
38
Learning Objectives in this Part of the Module
bull Understand how to access Content Providers asynchronously via the AsyncQueryHandler framework
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
39
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers bull Other ContentResolver
operations are still synchronous
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
40
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtml
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
41
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartDelete(int javalangObject Uri javalangString javalangString[])
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
42
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartInsert(int javalangObject Uri androidcontentContentValues)
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
43
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartQuery(int Object Uri String[] String String[] String)
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
44
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously bull startDelete () ndash Begins an
asynchronous delete bull startInsert () ndash Begins an
asynchronous insert bull startQuery () ndash Begins an
asynchronous query bull startUpdate () ndash Begins an
asynchronous update
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlstartUpdate(int Object Uri androidcontentContentValues String String[])
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
45
bull LoaderManager amp CursorLoader only provide a way to access results of async invoked query() operations on ContentResolvers
bull AsyncQueryHandler invokes all ContentResolver calls asynchronously
bull Async operations can be cancelled via cancelOperation()
Overview of AsyncQueryHandler
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
46
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonDeleteComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
47
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonInsertComplete(int javalangObject androidnetUri)
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
48
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonQueryComplete(int javalangObject androiddatabaseCursor)
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
49
bull Defines callback hook methods that are invoked in a handler thread when async operations invoked on a ContentResolver have completed bull onDeleteComplete () ndash Called
when async delete completes bull onInsertComplete () ndash Called
when async insert completes bull onQueryComplete () ndash Called
when async query completes bull onUpdateComplete () ndash Called
when async update completes
Overview of AsyncQueryHandler
developerandroidcomreferenceandroidcontentAsyncQueryHandlerhtmlonUpdateComplete(int javalangObject int)
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
50
bull Shows how to implement a ContentProvider that is accessed asynchronously
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
51
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
52
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
53
bull Shows how to implement a ContentProvider that is accessed asynchronously
bull Stores the DataRecord objects in a HashMap bull Supports all the ContentProvider ldquoCRUDrdquo
operations bull All of which are implemented as
synchronized Java methods bull Client Activity accesses the ContentProvider
using asynchronous two-way calls made via an AsyncQueryHandler bull Note the use of the Command
Asynchronous Completion Token amp Proactor patterns in this example
Example AsyncQueryHandler ContentProvider
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
54
public class ContactProviderActivityAsync extends ListActivity private SimpleCursorAdapter mAdapter abstract class CompletionHandler extends AsyncQueryHandler public CompletionHandler() super (getContentResolver()) abstract public void execute()
Example AsyncQueryHandler ContentProvider
This class implements the Command pattern (the execute() method) amp the Asynchronous Completion Token pattern (by virtue of inheriting from AsyncQueryHandler)
The adapter that binds our data to the Listview
Command hook method that must be overridden by subclasses
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
55
class InsertQueryHandler extends CompletionHandler private Object mNextCommand private String mValue = null public InsertQueryHandler (String value Object nextCommand) mValue = value mNextCommand = nextCommand public void execute() ContentValues v = new ContentValues() vput(data mValue) startInsert(0 mNextCommand MyCPCONTENT_URI v) public void onInsertComplete(int t Object nextCommand Uri u) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Invoke the async insert operation on the CONTENT_URI
Execute the next command when async insert completes
Store value to insert amp next command to execute when the async operation is done
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
56
class DeleteUpdateQueryHandler extends CompletionHandler private String mDeleteItem mUpdateItem mUpdateValue DeleteUpdateQueryHandler(String dI String uI String uV) mDeleteItem = dI mUpdateItem = uI mUpdateValue = uV public void execute() startDelete(0 (Object) new UpdateQueryHandler(mUpdateItem mUpdateValue) Uriparse (MyCPCONTENT_URI + mDeleteItem) (String) null (String[]) null) public void onDeleteComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store items to delete amp update as well as the value to update
Invoke the async delete operation passing in the next command
Execute the next command when async delete completes
Add mDeleteItem to the CONTENT_URI
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
57
class UpdateQueryHandler extends CompletionHandler private String mUpdateItem mUpdateValue UpdateQueryHandler(String updateItem String updateValue) mUpdateItem = updateItem mUpdateValue = updateValue public void execute() ContentValues v = new ContentValues() vput(data mUpdateValue) startUpdate(0 (Object) new QueryQueryHandler() Uriparse (MyCPCONTENT_URI + mUpdateItem) v (String) null (String[]) null) public void onUpdateComplete(int t Object nextCommand int r) ((CompletionHandler) nextCommand)execute()
Example AsyncQueryHandler ContentProvider
Store item amp value to update
Invoke the async update operation
Add mUpdateItem to the CONTENT_URI
Execute the next command when async update completes
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
58
class QueryQueryHandler extends CompletionHandler public void execute() startQuery(0 null MyCPCONTENT_URI (String []) null (String) null (String[]) null (String) null) public void onQueryComplete(int t Object command Cursor c) String[] cols = _iddata int[] ids = RididString Riddata mAdapter = new SimpleCursorAdapter(ContactProviderActivityAsyncthis Rlayoutlist_layout c cols ids) setListAdapter(mAdapter)
Example AsyncQueryHandler ContentProvider
Invoke the async query operation
Display the results when the query completes
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
59
public void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState) new InsertQueryHandler(Value1 new InsertQueryHandler(Value2 new InsertQueryHandler(Value3 new DeleteUpdateQueryHandler(1 2 Value4))))execute()
Example AsyncQueryHandler ContentProvider
Insert Value1 Value2 amp Value3 into ContentProvider then delete item 1 amp change the value of item 2 to Value4
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
60
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull Itrsquos not widely used in Android though the MMS app uses it extensively
packagesappsMmssrccomandroidmmsuiSearchActivityjava
AsyncQueryHandler mQueryHandler mQueryHandler = new AsyncQueryHandler(cr) protected void onQueryComplete (int token Object cookie Cursor c) mQueryHandlerstartQuery(0 null searchUri null null null null)
When query completes cons up a new CursorAdapter to display the results
Initiate a query for MMS threads that match the search string
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
61 wwwdrevanderbiltedu~schmidtPDFproactorpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash Split an Apprsquos functionality into async operations that perform activities on event sources amp completion handlers that use the results of async operations to implement App business logic
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-
Android Content Providers Douglas C Schmidt
62 wwwdrevanderbiltedu~schmidtPDFACTpdf
Summary bull AsyncQueryHandler is a helper class that helps make handling async
ContentResolver queries easier bull AsyncQueryHandler implements several patterns
bull Proactor ndash bull Asynchronous Completion Token ndash allows an App to efficiently demultiplex
amp process the responses of async operations it invokes on services
- Slide Number 1
- Learning Objectives in this Part of the Module
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Async Access to Content Providers
- Summary
- Summary
- Slide Number 13
- Learning Objectives in this Part of the Module
- Overview of Loader
- Overview of Loader
- Overview of LoaderManager
- Overview of LoaderManager
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Overview of CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Using LoaderManager amp CursorLoader
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- Example of LoaderManager ContentProvider
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- ContactProviderActivityAsync Example
- Summary
- Summary
- Slide Number 37
- Learning Objectives in this Part of the Module
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Overview of AsyncQueryHandler
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Example AsyncQueryHandler ContentProvider
- Summary
- Summary
- Summary
-