Examples of how AsyncTask is used in PSLab Android App
In this blog, we will look at a very useful and important feature provided by Android - AsyncTask and more importantly how AsyncTasks have been put to use for various functionalities throughout the PSLab Android Project What are Threads? Threads are basically paths of sequential execution within a process. In a way, threads are lightweight processes. A process may contain more than one threads and all these threads are executed in parallel. Such a method is called “Multithreading”. Multithreading is very useful when some long tasks need to be executed in the background while other tasks continue to execute in the foreground. Android has the main UI thread which works continuously and interacts with a user to display text, images, listen for click and touch, receive keyboard inputs and many more. This thread needs to run without any interruption to have a seamless user experience. When AsyncTask comes into the picture? AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. In PSLab Android application, we communicate with PSLab hardware through I/O(USB) interface. We connect the PSLab board with the mobile and request and wait for data such as voltage values and signal samples and once the data is received we display it as per requirements. Now clearly we can’t run this whole process on the main thread because it might take a long time to finish and because of that other UI tasks would be delayed which eventually degrade the user experience. So, to overcome this situation, we use AsyncTasks to handle communication with PSLab hardware. Methods of AsyncTask AsyncTask is an Abstract class and must be subclassed to use. Following are the methods of the AsyncTask: onPreExecute()Used to set up the class before the actual executiondoInBackground(Params...)This method must be overridden to use AsyncTask. This method contains the main part of the task to be executed. Like the network call etc.The result from this method is passed as a parameter to onPostExecute() methodonProgressUpdate(Progress...)This method is used to display the progress of the AsyncTaskonPostExecute(Result)Called when the task is finished and receives the results from the doInBackground() method There are 3 generic types passed to the definition of the AsyncTask while inheriting. The three types in order are Params: Used to pass some parameters to doInBackground(Params...) method of the Task Progress: Defines the units in which the progress needs to be displayed/Result : Defines the data type to be returned from onInBackground() and receive as a parameter in the onPostExecute(Result) method Example of the usage of the AsyncClass is as under : private class SampleTask extends AsyncTask<Params, Progress, Result> { @Override protected Result doInBackground(Params... params) { // The main code goes here return result; } @Override protected void onProgressUpdate(Progress... progress) { // display the progress } @Override protected void onPostExecute(Result result) { // display the result } } We can create an instance of this class as under and execute…
