In an effort to learn Android I am writing a small app. The first thing I am trying to do is login via a remote API.
I would like to show a “loading” dialog when the call is being made (in case he user in using mobile internet). Researching this has shown two possible methods.
One is to use a ProgressDialog and a private class that extends Thread, the other is using a private class that extends AsyncTask.
Which is best/more appropriate for this task?
I have tried using the ProgressDialog version but am struggling. I have put the function making the http request in the extended Thread run() method, but am unsure on how to pass the response data (JSON) back into my activity.
Any and all help gratefully received.
The best way possible is to use an
AsyncTaskwith aProgressDialog. You should extendAsyncTaskand implement all the methods you need:onPreExecute()– here you initialize yourProgressDialogandshow()itdoInBackground()– here you do your workonPostExecute()– here you calldismiss()onProgressDialogto hide itonProgressUpdate()– here you can change the progress of yourProgressDialogif it’s determinateThere is a
get()method inAsyncTaskclass that lets you retrieve the result of the work. Also you can implement an interface between theAsyncTaskand callingActivityto return the result. Hope this helps.