android:这段代码中的线程如何能够直接访问UI元素

时间:2016-06-22 13:10:10

标签: android multithreading

我有一个活动,它会更新媒体播放器的搜索栏。我在互联网上找到了这个代码。

public class SampleDownload extends AppCompatActivity implements Runnable {
    private Seekbar seeekbar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        startMedia = (Button) findViewById(R.id.button1);
        stopMedia = (Button) findViewById(R.id.button2);
        startMedia.setOnClickListener(this);
        stopMedia.setOnClickListener(this);
        seekBar.setOnSeekBarChangeListener(this);
        seekBar.setEnabled(false);
    }

       ...... code contiunes not shown here

    @
    Override
    public void run() {
        int currentPosition = mp.getCurrentPosition();
        int total = mp.getDuration();

        while (mp != null && currentPosition < total) {
            try {
                Thread.sleep(1000);
                currentPosition = mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }
            seekBar.setProgress(currentPosition);
        }

    }




}

如果此线程在除UI之外的单独线程上运行。它是如何改变搜索栏的?

0 个答案:

没有答案