How can you irritate the hang of the application know all of us. Now imagine that your Android app has some time-consuming action, just to get some data or an image from the web. If you do this in the main thread of the program, the UI will stop responding, and the user of your program will want you just what you probably want sometimes with Internet Explorer developers.

To get that, simply glue the button to the screen, and as a response.

 

And as you type it, type the following code:

In fact, we are only interested in lines 24-32. The rest is a shell. As we see, the Thread.sleep (10000) statement is called when the button is pressed. This will make the application wait 10 seconds. Try this on any device and eg call the program menu (including the dedicated menu button). You will see that nothing happens, the app simply hangs.

So you have to separate a separate thread and let it go in the background to accomplish time-consuming tasks. You can do it “traditionally” after javowy:

You can run this modified program and you will find that the background thread does not block other interface elements. Of course, there is “but”. If you try to access the code from any of the visual components in this thread, you’ll get an exception:

I think the message of this exception is understandable.

For Android, a completely different multithreading mechanism was created. We will use here the AsyncTask class, which as the name implies, serves asynchronously. We will also use one interesting gadget – ProgressBar. It is available in the Form Widgets palette.

 

We want the user to see that the program does something and not just hang up. Such ProgressBar is visible by default and animation is performed. We will do so at first not visible, we will show it when we release the thread and hide when we finish it.

Using the aforementioned AsyncTask class implies creating a class that inherits and implements some methods. To the class of our activity, I add an internal class to a separate thread inheriting from AsyncTask. Nothing prevents it from being a separate class at all. There was also something new here – the <Void, Void, Void> element, and the odd parameterization of the methods inInBackground and onPostExecute. This is related to the concept of generic classes. This issue will not be dealt with here, it is not directly related to the multi-threaded topic in Android. Objects of the IndividualWindow class will be separate threads within the application. Note that there are three methods that override such methods from the class we inherit. The onPreExecute method is automatically called upon thread startup. I am showing here our ProgressBar. The circle becomes visible and begins to “work”. I also give a message to the console. In this method, and in the onPostExecute method we can safely refer to the graphical components in our activity. We do not do this in the method of InBackground. What’s going to happen within the thread – these long-term actions are just written in the InBackground method. Here I am throwing information on the console, and I insert a 10 second wait. The onPostExecute method is invoked automatically at the end of the thread. I’m showing here the finish and hiding ProgressBar. We do not do this in the method of InBackground. What’s going to happen within the thread – these long-term actions are just written in the InBackground method. Here I am throwing information on the console, and I insert a 10 second wait. The onPostExecute method is invoked automatically at the end of the thread. I’m showing here the finish and hiding ProgressBar. We do not do this in the method of InBackground. What’s going to happen within the thread – these long-term actions are just written in the InBackground method. Here I am throwing information on the console, and I insert a 10 second wait. The onPostExecute method is invoked automatically at the end of the thread. I’m showing here the finish and hiding ProgressBar.

Let’s now look at what’s happening on the onCreate method:

In line 55 I start hiding ProgressBar. This will only be displayed while the thread is running in the background. As a response to pressing the button, I run our thread (line 60).

The program after launching and clicking the button looks like this:

LogCata console content after completion of the entire process: