There are several ways to call MXQuery from Android. This document explains the most common ways of using MXQuery in Android with code examples for each.
Calling XQuery asynchronously from Android is the preferred method. There are several implementation options for asynchronous XQuery processing. We outline a few of the most common options here:
There many options for how to implement a service for XQuery. This example uses a local bound service with callback functions:
First define an interface which will serve as the callback for the asynchronous query execution:
Then define the Service:
Then define the Activity:
Download sample project: ServiceExample Project
Android's AsyncTask class can make it easy to perform long-running operations in the background. However, for actions that require UI updates, AsyncTasks can be difficult to work with in combination with the Activity lifecycle. Therefore we generally recommend running MXQuery in a Service rather than in an AsyncTask. If AsyncTask is the right choice for your application, this example will help get you started:
It is generally recommended to use MXQuery
on Android asynchronously, however it is also possible to call MXQuery
syncronously. Be aware that long-running XQuery queries may cause the
application to become unresponsive (if run in the main UI thread); see Designing for Responsiveness for more information.
This simple example runs a query in an Activity and returns the result as a String, which is displayed in the Activity's view.
Download sample project: XQJExample Project