Avoid performing long-running operations (such as network I/O) directly in the UI thread — the main thread of an application where the UI is run — or your application may be blocked and become unresponsive. Here is a brief summary of the recommended approach for handling expensive operations:
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.- Create a Handler object in your UI thread
- Spawn off worker threads to perform any required expensive operations
- Post results from a worker thread back to the UI thread's handler either through a Runnable or a
Message
- Update the views on the UI thread as needed
So Here is the best solutions:
ou have to move the portion of the background task that updates the ui onto the main thread. There is a simple piece of code for this:
runOnUiThread(new Runnable() {
@Override
public void run() {
//stuff that updates ui
}
});
Documentation for
Activity.runOnUiThread.
or you may use this:
Basically you would wrap //do whatever you wantin a Runnable
and invoke it with a Handler
instance.
Handler refresh = new Handler(Looper.getMainLooper());
refresh.post(new Runnable() {
public void run()
{
//do whatever you want
}
});
or simple do that:
you don't call directly the onProgressUpdate
, you have to call publishProgress
and let the AsynTask framework to handle the onProgressUpdate
to be called back on the UI thread.
Finally, google it : Here is the best solutions that reach there. Gooogled
Happy Coding!!!