0

I have a an Activity which updates the seekbar for the mediaplayer. I found this code on internet.

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);
        }

    }




}

If this thread is running on a seperate thread other than the UI. how is it able to change seekbar

Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • 1
    Check out this question: http://stackoverflow.com/questions/11123621/running-code-in-main-thread-from-another-thread – Rúben Sousa Jun 22 '16 at 13:13
  • But how come without using any hanldler, the code is working in this case – Santhosh Jun 22 '16 at 13:16
  • `runOnUiThread(new Runnable() { public void run() { //Do something on UiThread } });` Source [link](http://stackoverflow.com/questions/11140285/how-to-use-runonuithread) – ArbenMaloku Jun 22 '16 at 13:22
  • I understand all that things. But here without any such need the code is working. the thread is able to pass things. Thats what is suprising. So how this code is working. I am not how to make this code work. This is a working code. – Santhosh Jun 22 '16 at 13:26
  • Is your question "why is this code working although the docs say don't update gui from a non-gui-thread" ? – k3b Jun 22 '16 at 13:53
  • Yes. thats exactly is my question – Santhosh Jun 22 '16 at 14:47
  • How is the run() method started inside your activity? It seems like you're already running it in the main thread. – Rúben Sousa Jun 22 '16 at 23:40
  • because when i implement runnable it got added automatically – Santhosh Jun 23 '16 at 04:55

0 Answers0