Update Ui Asynchronously In Android
I have a webservice call which is called every 10 seconds and should update a TextView with the webservice reply(or atleast show a toast message every 10 seconds) But the UI is not
Solution 1:
As everyone has mentioned, you're making network calls in the UI thread and performing Thread.Sleep() which freezes your UI.
I'd try something like this AsyncHttpClient class, it has all the functionality you need, you'll have to perform your UI updates in the callback.
http://loopj.com/android-async-http/
Solution 2:
Here is an example of an AsyncTask
publicclassTalkToServerextendsAsyncTask<String, String, String> {
@OverrideprotectedvoidonPreExecute() {
super.onPreExecute();
}
@OverrideprotectedvoidonProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
@OverrideprotectedStringdoInBackground(String... params) {
//do your work herereturn something;
}
@OverrideprotectedvoidonPostExecute(String result) {
super.onPostExecute(result);
// do something with data here-display it or send to mainactivity
}
Then you can access by calling
TalksToServervarName=newTalkToServer(); //pass parameters if you need to the constructor
varName.execute();
Async DocsProgress Dialog Example
You don't want to do network stuff or call sleep
on the UI
thread. If it is an inner class then you will have access to your member variables of the outer class. Otherwise, create a contructor in the AsyncTask
class to pass context
if you want to update from onPostExecute
or other methods besids doInBackground()
.
Post a Comment for "Update Ui Asynchronously In Android"