- activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
- MainActivity.java
package kr.co.devinside.mythread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
ProgressBar bar = null;
TextView txtView = null;
boolean isRunning = false; ProgressHandler progressHandler = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressHandler = new ProgressHandler();
bar = (ProgressBar)findViewById(R.id.progressbar);
txtView = (TextView)findViewById(R.id.txtView);
}
public void onStart() {
super.onStart();
bar.setProgress(0);
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
for(int i = 0; i < 20 && isRunning; i++) {
Thread.sleep(1000);
Message msg = progressHandler.obtainMessage();
progressHandler.sendMessage(msg);
}
} catch (Exception e) {
}
}
});
isRunning = true;
thread1.start();
}
public void onStop() {
super.onStop();
isRunning = false;
}
public class ProgressHandler extends Handler {
public void handleMessage(Message msg) {
bar.incrementProgressBy(5);
if(bar.getProgress() == bar.getMax()) {
txtView.setText("Done");
} else {
txtView.setText("Working ..." + bar.getProgress());
}
}
}
}
'Smart Device > Android' 카테고리의 다른 글
Thread - AsyncTask 사용하기 (1) | 2015.08.10 |
---|---|
Thread - Handler를 이용해 Runrable 객체를 실행하기 (0) | 2015.07.30 |
Thread - Java의 Thread를 사용 (0) | 2015.07.30 |
SlidingPage와 GestureDetector (0) | 2015.07.15 |
에뮬레이터에서 단말의 방향을 전환하는 단축키 (0) | 2015.07.08 |