Explaining the PDF Splitter’s Android AsyncTask

Up to this point, we’ve spent a couple of posts explaining how to develop a practical Android PDF Splitter.

It is a common practice to encapsulate real world things into data structures such as lists, arrays or binary trees. So firstly we identified and coded the logic required to transform the user input (a String) into a Java data structure (a Java ArrayList of Integer values) in order for us to comfortably treat and process the data.

Explaining the PDF Splitter’s Android AsyncTask

And secondly, we ended up implementing the domain logic in an Android’s AsyncTask class named SplitPDFAsyncTask. Recall that the so-called domain logic, also referred to as business logic, is the core stuff of your app or, put another way: The problem to be solved, the important logic that the application is supposed to run.

Since you may wonder why we put the domain logic in the SplitPDFAsyncTask class, let’s today describe how this class is implemented.

What Is AsyncTask?

By default, a regular Android application runs on a single main UI thread. This is called synchronous programming which means that the code instructions are executed sequentially one by one, step by step.

The problem with synchronous programming is that it may lead to bottlenecks in some cases, especially when there are a number of operations to be run whose execution time is unknown a priori.

Let’s use the example below to understand this:

  • Instruction 1; (takes one second to be run)
  • Instruction 2; (takes ? Seconds to be run)
  • Instruction 3; (takes 0.5 seconds to be run)
  • Instruction 4; (takes ? seconds to be run)
  • Instruction 5; (takes 0.25 Seconds to be run)

What Is AsyncTask?

Let’s assume that Instruction 2 is an HTTP request that takes x seconds to query a web service and process the corresponding JSON response. Instruction 4 could be a PDF download, to give another example.

In this case, the application will remain blocked indefinitely until the instructions 2 and 4 are completed.

AsyncTask comes to the rescue in these scenarios because enables to easily run an asynchronous task on a background thread.

Why Does the PDF Splitter Use an AsyncTask?

Since PDF files range in size from a few kilobytes to many megabytes, we don’t know how long it will take to split a document; it can actually vary from a number of milliseconds to a few seconds.

It is for that reason that AsyncTask is used. Android Developer’s Reference states that AsyncTasks should ideally be used for short operations (a few seconds at the most.)

How Is SplitPDFAsyncTask Implemented?

Android’s AsyncTask consists of four steps:

The life cycle of an AsyncTask object is well documented, so please click on the links above and read the Android documentation on this subject.

The most important step is doInBackground which is used to perform the background computation (split a PDF document) that may take a long time.

On the other hand, it is crucial to understand that onPostExecute(Result) is run on the main UI thread just after the doInBackground (Params...) method has been completed on the background.

In our example:

onPreExecute() method

A ProgressDialog is shown on the UI thread.

Figure 1. This ProgressDialog is shown just before the background task is executed

Figure 1. This ProgressDialog is shown just before the background task is executed

doInBackground (Params...) method

This is where the business logic is placed. The PDF document is split on this background task.

onProgressUpdate (Progress...) method

As you see, there’s no onProgressUpdate (Progress...). We don’t use any progress bar while the background task is being executed, but we simply display a ProgressDialog.

onPostExecute (Result) method

As we noted above, onPostExecute(Result) is run on the UI thread once the background task is finished. This logic simply hides the ProgressDialog and just after displays an AlertDialog informing the user about the result of their split operation.

Figure 2.  AlertDialog informing about the result of the operation

Figure 2. AlertDialog informing about the result of the operation

getPages (String) method

And finally, the regex-based algorithm that we explained in this post is coded in the getPages (String) method.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *