Android vs. asynchronous call handlers

I am currently using Handlers to call web service methods so that it runs in the background. The problem is that it has more time to give an answer, it seems more expensive in terms of performance. Now I plan to go to Async Calls, which will be the best? What are the differences between handlers and asynchronous calls in Android? Please help me come up with a better solution.

For reference, I give a few code snippets here

signIn.setBackgroundResource(R.drawable.signin_press); changeImage=new Runnable(){ public void run(){ signIn(); } }; signinHandler.post(changeImage); 

When I click the Login button, I call this method, it looks like the user interface hangs for several minutes before the method is called. This method involves two costly web service calls to authenticate and register a user. How can I normalize the slowness of the application. Help me.

+4
source share
2 answers

There are certain advantages to using Thread and Handler , respectively, using AsyncTask , it really depends on your use, and profiling these advantages against harm is likely to come to you.

I would recommend the Painless Threading article for a little understanding of threads on Android.

EDIT for more information.

If we adapt the code from the article “Painless Thread” that was linked, you might get something like this.

  new Thread(new Runnable() { public void run() { signIn(); signinHandler.post(new Runnable() { public void run() { //TODO: Something to notify of login complete / continue processing. } }); } }).start(); 

In TODO, you need to continue or notify the execution, I don’t know what is currently being processed in signIn() , so if this crosses the UI thread, it will need to be reorganized.

+3
source

AsyncTask uses a thread pool and handlers internally. This is not magic; see source . Performance will not be noticeably better than your own handlers (except for the fact that using a thread pool can save a little overhead for creating a new thread, but it is pretty small compared to the duration of typical web service calls; a few milliseconds certainly not will have a noticeable effect on the user). Given the number of factors involved in creating a web request, what would make you think that the thread / handlers are slowing down your application (unlike your network connection, server traffic, etc.)? Profiles your code back this statement?

+2
source

Source: https://habr.com/ru/post/1343645/


All Articles