In the Oscilloscope Activity, communication with the PSLab device goes in parallel with updations of the graph which result in inoperable UI if both these functions are performed in the main thread. This would severely degrade the user experience. In order to avoid this, we simply used AsyncTask. AsyncTasks are used to perform communications with the device in the background thread and update UI when the task in background thread completes. AsyncTask thus solves the problem of making UI super laggy while performing certain time-consuming functions. The UI remains responsive throughout.
More about AsyncTask
AsyncTask is an abstract Android class which helps the Android applications to handle the Main UI thread in a more efficient way. AsyncTask class allows to perform long lasting background operations and update the results in UI thread without affecting the main thread.
Implementing AsyncTask in Android applications
- Create a new class inside Activity class and extend AsyncTask:
- Execute the task:
How they are used in Oscilloscope Activity?
The following diagram explains how AsyncTasks are used in Oscilloscope Activity.
AsyncTask in PSLab Android App
A public class extending AsyncTask is defined, this task is executed from another thread.
doInBackgroundMethod performs the part related to communication with the PSLab device.
Here we are capturing the data from the hardware using captureTraces and fetchTraces method.
After the thread, completely executed onPostExecute is called to update the UI/graph. This method runs on the main thread.
This simply solves the problem of lags and the Oscilloscope works like a charm.