Processing UI from another topic

I have a class that extends Runnable. This class does the hard work (basically uploads the image from the network) to another thread. I want to update the interface (display the loaded image on ImageView) from this class. I tried using a handler but could not. Here is my code:

class DataReceiver implements Runnable
{
  public Bitmap bmp;
  Public Handler uiHandle;
  @Override
  public void run()
  {
    while(true)
    {
      //do image download process here
    }
  }
}

In the main action

ImageView img = (ImageView)findViewById(R.id.dispImg);
DataReceiver dr=new DataReceiver();

Handler uiHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        updateUI();
    }
}

dr.uiHandle = uiHandler;
(new Thread(dr)).start();

pubic void updateUI()
{
    img.setBitmap(dr.bmp);
}

Is it the right method to update an interface?

+3
source share
3 answers

nearly; D you need the class thread to add this line before doing:

while(true)
    {
      //do image download process here
    }
uiHandle.sendEmptyMessage(0);
+2
source

AsyncTask, , run() doInBackground, onPostExecute.

+3

To update the user interface from another thread, use

activity.runOnUIThread(new Runnable());
0
source

All Articles