开发者

Thread in Java/Android

开发者 https://www.devze.com 2023-01-19 19:24 出处:网络
Greetings. How can I send two parameters into a thread and run methods when the user pushes buttons on the display.Having my variables in the UI thread didn\'t work and were erased when the UI thread

Greetings.

How can I send two parameters into a thread and run methods when the user pushes buttons on the display. Having my variables in the UI thread didn't work and were erased when the UI thread randomly restarts.

UI T开发者_JS百科hread ______________ Other Thread

User presses button -------> Run Method

Cheers.


I think you should just define listeners for your buttons. And those listeners should in turn, launch threads with the paremeters you want, to perform other tasks, while keeping the UI available and responsible to the user again, while those threads are performed. You need something like this:

public void onClick(View v) {
  new Thread(new Runnable() {
    public void run() {
      Bitmap b = loadImageFromNetwork();
      mImageView.setImageBitmap(b);
    }
  }).start();
}

This example was taken from the Android Developers Blog, where you can get other helpful information and hints.


Have a look at this blog article. It describes how to keep a thread working across screen rotation.

0

精彩评论

暂无评论...
验证码 换一张
取 消