20.11.2016 Views

ANDROID APP

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

วิดเจ็ตอื่นๆ: จากการใช้ปุ่มมาถึงการใช้ Seek Bar<br />

113<br />

public class HandlerUpdateUi extends Activity {<br />

private static ProgressBar m_progressBar; //UI reference<br />

int percent_done = 0;<br />

final Handler mHandler = new Handler();<br />

// Create runnable for posting results to the UI thread<br />

final Runnable mUpdateResults = new Runnable() {<br />

public void run() {<br />

m_progressBar.setProgress(percent_done);<br />

}<br />

};<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

setContentView(R.layout.main);<br />

m_progressBar = (ProgressBar) findViewById(R.id.ex_progress_bar);<br />

}<br />

Button actionButton = (Button) findViewById(R.id.action);<br />

actionButton.setOnClickListener(new View.OnClickListener() {<br />

public void onClick(View view) {<br />

do_work();<br />

}<br />

});<br />

//example of a computationally intensive action with UI updates<br />

private void do_work() {<br />

Thread thread = new Thread(new Runnable() {<br />

public void run() {<br />

percent_done = 0;<br />

mHandler.post(mUpdateResults);<br />

computation(1);<br />

percent_done = 50;<br />

mHandler.post(mUpdateResults);<br />

}<br />

computation(2);<br />

percent_done = 100;<br />

mHandler.post(mUpdateResults);<br />

}<br />

});<br />

thread.start();<br />

final static int SIZE=1000; //large enough to take some time<br />

double tmp;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!