You get a bonus - 1 coin for daily activity. Now you have 1 coin

Features of Service and AsyncTask in Android applications

Lecture



The purpose of the article is to explain the features of using Service and AsyncTask in Android applications. The material assumes that the reader has some experience in developing applications for Android.

Task

Everyone writing under Android knows about UI thread. By default, all processes run in this thread, and the application has no more than five seconds to perform complex calculations. Just five seconds ... But imagine that the application on your phone is "frozen" and does not respond to your actions for as long as five seconds, especially if you receive an important call. This is extremely unpleasant. To avoid this, all long operations must be performed in separate threads.

For asynchronous task execution, besides the standard java.util.concurrent package, Android provides its own approaches for working with threads: Service and AsyncTask.

What is a service? It is one of the four main components of the system, allowing you to perform a task in the background without the need for user input. That is, it is a way to do something while the user is working with our application - with one of our Activity. Since Service performs the task in the background, a false impression is created that the work is proceeding in a separate thread, but this is not so: the usual Service works in UI thread, and for long calculations it is still necessary to generate a thread.

Android developers thought about us and provided an IntentService. It independently launches a stream on each came Intent. There is only one flow, so all incoming Intent-objects will be queued and executed sequentially one after the other.

AsyncTask, like IntentService, allows you to perform a task in a separate thread. But, unlike the IntentService, AsyncTask has access to the UI thread - the context of our Activity and, accordingly, the interface, which allows us to update the screen directly from the class.

It would seem that everything is simple: if you need to perform a long operation, we write Service, if we also need interaction with the interface, we write AsyncTask ... but the devil, as they say, is in the details ...

Loss of context

AsyncTask depends on the context of the Activity from which it was launched. That is why he can update the screen. However, this is also a weakness of AsyncTask, since the context may be lost . For example, when you rotate the screen, press the BACK key or receive an incoming call. In these cases, the system will destroy our Activity and create anew, but the context in the previously launched AsyncTask will remain the same, that is, after performing work in the stream, our class will try to access an interface that no longer exists .

To avoid this, you need to somehow check the relevance of the context in our AsyncTask, which complicates the logic of our application. Wouldn't it be better to spend a little more time and implement an IntentService?

  Features of Service and AsyncTask in Android applications

When you try to access Activity, our application will crash.

Duplicate queries

Imagine that each time you return to an Activity, you need to perform some actions in AsyncTask and refresh the screen. That is, every time the user reverses the screen, the system will destroy the Activity and re-create at the top of the stack, thereby re- running the code in AsyncTask.

What happens to our application? Incorrect behavior or system crash? Hard to say. The problem with these kinds of errors is that they are difficult to catch. When developing a project on an emulator, you usually do not rotate it, and this type of error goes unnoticed. It is good if there is a team of testers who will help to catch such non-obvious errors before passing the application to users.

Single launch

Another AsyncTask property - one instance of the class can only be run once. If you need to execute the necessary code several times in a separate thread using AsyncTask, you will have to create the corresponding number of objects.

This will not work:

MyAsyncTask task = new MyAsyncTask ();
for (int i = 0; i <1000; i ++) {
task.execute (someKindOfData);
}

But this code is possible:

for (int i = 0; i <1000; i ++) {
MyAsyncTask task = new MyAsyncTask ();
task.execute (someKindOfData);
}

Conclusion

You might get the impression that I am disapproving of using AsyncTask in projects. This is true, but in fairness it is worth noting that it is acceptable to use them to read data from a file or database, although in the latter case it is better to use AsyncQueryHandler.

Now you know a number of difficulties that AsyncTasks can use in your code. Service does not have these disadvantages. This is a separate component of the system, not tied to the context of your application, which allows you to fully separate the logic and display in your project.

In addition, when the Service will request more memory, you can always put it in a separate process in a manifest. Finally, the Service will allow monitoring requests to the server, their order. Use it!

created: 2016-08-02
updated: 2021-03-13
132367



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Mobile Programming (Android IOs)

Terms: Mobile Programming (Android IOs)