What is AsyncCallBack in Android? - android

Can anyone give me a clear description of what is AsyncCallBack? Does it exist in the latest android version? Because I am directed to AsyncTask when I search for AsyncCallBack.... Are these both same? Does anyone have an example?
Thanks in advance.

There is no class named AsyncCallback in android. I think what you are looking for is AsyncTask which is a way to run a piece of code on another Thread so the UI won't be blocked, and receive it's results on the UI thread. For example, say you want to talk to a server on the internet in response to the user clicking something in the UI, then receive some result from the server, and update the UI. AsyncTask makes doing this very easy compared to doing regular threading code because threading lifecycle and communication back to the UI thread is handled for you. As a bonus there is also support for canceling a background task, but you have to write the code to handle it when cancel is called. It doesn't do it without some work on your side.

I think the terms may be mixed up here, there isn't an AsyncCallback in Android (as far as I know). There is however very widely used AsyncCallback interface in GWT (Google Web Toolkit): http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/rpc/AsyncCallback.html.
In Android you use AsyncTask to easily run background operations asynchronously on a separate Thread from the main UI/app thread: http://developer.android.com/reference/android/os/AsyncTask.html.
Here is a good intro article on AsyncTask: http://developer.android.com/resources/articles/painless-threading.html.

Related

Do I ALWAYS need to use AsyncTask when using MySQL?

Hi guys I have a question about Asyntask which is used in android studio :
As far as I know AynTask is used for user interface via one thread, the so called UI Thread. If you perform a long running operation directly on the UI Thread, for example downloading a file from the internet, the user interface of your application will “freeze” until the corresponding task is finished.
But let's say that I want to register an account so that I can login, that shouldnt take time at all so why should I use Asyntask for this?
Let's say I want to send 100 strings to the Database, that can be done in milisecs I think, so again, why to use and how to decide when to use Asyntask?
I hope you guys can help me out, I have been searching for a long time !
If you don't know how much time operation will take, you should perform it in a separate thread and then pass the results to UI thread. I think the database should be accessed in a separate thread as well as HTTP requests. In the case of time-consuming query, it may be a long operation. AsyncTask is one way to do it. You can also use other techniques. The popular technique used nowadays is applying RxJava library, which gives you the high-level functional reactive interface for writing multi-threaded applications with a few additional features. You can perform an operation in e.g. Sechdulers.io() (I/O) thread and then pass the result to AndroidSchedulers.mainThread(), which is UI thread.
There are also other techniques like using Looper & Handler from Android SDK or using Thread class from Java, but such techniques require more knowledge, more work, writing more boilerplate code & you have more problems to deal with.

How to use thread and handler to hit Web API?

My Boss told me Not to use AsyncTask<> to hit a webservice. Standard told me use Threads to hit a webservice and then use the Handler to process the response from Thread. Can someone ellobrate how to use thread and handler to hit a API and then give the control back to the Handler , atleast a Tutorial if its possible or Code. What is handler, is there only one Handler in android Application? thnks
Please check it below example:
http://androidexample.com/Thread_With_Handlers_-_Android_Example/index.php?view=article_discription&aid=58
What you are actually trying to do with Thread and update your GUI with Handler is same as using AsyncTask. AsyncTask considers most of the aspects which are somehow difficult to manage. Obviously there are some downsides to use AsyncTask for more details have a look at my answer Down sides of AsyncTask
When to use?
If you're supposed to perform a short time request, then AsyncTask is perfect. However, if you need to get some data and display it but you don't want to worry about whether to download again if the screen is rotated and so on, you should consider using an AsyncTaskLoader.
If you need to download some big data, then you can consider using an IntentService.

Async task or threads

I have the following task:
I have to do a Wi-Fi scan and then send some RSSs to the a server to obtain a location from the server. After that I should display the location on a map.
I have one activity in my application. What is the best way to implement it? I haven't implemented a thread nor AsyncTask in android before. So I don't know a lot about it. I have read about threads and AsyncTask on android developer website. But I still can't understand everything.
I would appreciate it if anyone can tell me when its the best to use threads, and how AsynTask is different from threads and when to use AsyncTask instead of threads.
EDIT: the task that I have to implement should run only when the user click on actioBar item
.
If you can't understand the read documentation, I'd suggest starting with an AsyncTask, as it already implements a Thread when doInBackground() is fired and it already implements some mechanisms that you would probably have to implement by hand when using Threads (concurrency, Thread start/stop processes, sending information to UI thread, ...).
However, you don't specify what exactly want to do. If you plan to do a long-running process, instead of Threads or AsyncTasks, it's recommended using Service that would start a Thread as implementation, as contrary to popular beliefs, AsyncTasks are meant for short-lasting tasks.
In any case, if you're starting with this, I'd recommend using AsyncTask first, and when understood what it's doing and how, decide by yourself what's the best way to implement what you're planning to do: if running a Service with a Thread inside, a Thread itself or an AsyncTask.

AsyncTask or Handler- Which one is better for time consuming network interaction and processing?

I am new to android development. Currently i am working on an application which will take a query from user, send the query to the server and will receive an XML response in return. Then it will process the xML response using XMLparser, will extract the answer from XML response and display answer to the user. Since all this processing takes some time, i want to show a progress dialog to the user for the processing time it takes.
I have gone through some ways of doing this like i can use AsyncTask, Handler or Runnable (java) threads. But as much I have read about all these, I have got confused which one to use.
Please tell me which one is better to use for above mentioned scenario. And a simple way to implement the preferred one.
Thank You in advance.
I'd recommend you to use AsyncTask because it is simplier than other approaches and it suits your needs.
I think you mess a bit these three different entities:
AsyncTask runs a set of actions in a separate thread, can show progress during its work and show notification when the task is completed.
Runnable is a command that can be performed. You should run in a separate thread. Also you should develop a logic in this case how to update progress and how to notify when the task is finished.
Handler is a special class that can handle messages that are sent to the handler's thread.
From http://developer.android.com/reference/android/os/AsyncTask.html :
This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
AsyncTask is designed for a use very similar to yours.
Runnable is only to run a block of code on another thread:
Represents a command that can be executed. Often used to run code in a different Thread.
(http://developer.android.com/reference/java/lang/Runnable.html)
Handler is used more for Message Queuing. Your case doesn't seem to require messages being sent repeatedly.
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.
http://developer.android.com/reference/android/os/Handler.html
Note that neither Runnable nor Handler talk about displaying the results back on the UI thread. That is where AsyncTask helps you more than either of these.
It may be my personal preference - but I would use AsyncTask in your case. It provides all necessary controls for starting up the task, updating progress as necessary, etc. I have a very similar requirement in my app (send request to server, get response in XML, parse response, do something with the data) - I'm using AsyncTasks for this purpose.
As far I know AsyncTask is the recommended way. I think is the easiest way to implement and the more "Android best practice" for asynchronous tasks.
Could you refer to this question
Here's how I see it.
Handler is more for queuing many actions, and gives a bit more control. It's better for repetitive tasks which are generally not restricted to the UI.
AsyncTask provides a simple way to do background processing, not caring much about the lower-level stuff. It's great for relatively small, individual UI updates.
IMO, you should use AsyncTask. That being said, it's kind of a toss-up.
I think it's a matter of self-preference, but in your case I would go for the AsyncTask because it facilitates the interaction between the UI thread and the background thread.
I'd use a combination of AsyncTask and Handler, because please remember that you cannot change the UI from outside the UI thread (in this case you cannot intervene and show the answer to the user).
To overcome this, I ran the AsyncTask and catched the result with a custom callback method, which simply encapsulate it inside a Message and sends it to my custom Handler, which is inside the UI thread and can safely render on-screen my result.
AsyncTask might be the choice,because it provides all necessary controls for starting up the async task, updating progress bar, etc.
But, the point is AsyncTask is the best solution to the scenario.
Handler or Runnable are more suitable to duplex cases, like chat apps.

asynctask in Android

Can anybody tell about the "asynctask" used in android application. Currently I am working on an application where I have to create a Class in which I have to just get the response of any particular URL given.
I this particular class I was told to perform this task by making use of "asynctask". I had been getting very quick responses of all my Questions from here so far and I am greatly obliged to all of them who are helping me since my first question I posted here.
I am quite new to Android Programming and feeling a bit confidence by the community's camouflage with me.
Thanks,
david
Google's documentation for AsyncTask is pretty excellent: http://developer.android.com/reference/android/os/AsyncTask.html
It's basically a construct that makes threading very simple. When you are doing something like making a web request to some URL, you don't want to block the UI thread. However, you usually want to update your UI with the results once your background task has completed.
AsyncTask makes this easy. First, you need to create a class that extends AsyncTask. When you execute the task, its doInBackground method will be called on a background thread. This is where you can download something from the Web or do whatever else you need to do.
The return value from doInBackgroundwill be provided to the onPostExecute method, which can update the UI appropriately.
Again, I recommend checking out Google's documentation. They've got great examples on how to use this.

Categories

Resources