== Handler
1. 임의의 시간에 무엇을 처리하고 싶을 때
2. 일정한 주기를 반복적으로 수행할 때
3. 시계 또는 타이머 또는 애니메이션, 백그라운드 감시 작업
4. 주간에 멈춤 가능
간단 방법)
Handler h = new Handler(){
public void handleMessage(Message m){
//기능
h.sendEmptyMessageDelayed(메시지, 초);
}
};
h.sendEmptyMessage(0);
== CountDownTimer(인터페이스)
1. 주기적인 작업처리 할 때 사용
2. 중간에 멈추게 할 수 있음
간단 방법)
CountDownTimer cdt = new CountDownTimer(long l, logn l2){ // l : 총시간 , l2 : 카운트 주기
public void onTick(long m){
//주기적인으로 실행할 기능
}
public void onFinish(){
//끝날때 기능
}
}.start();
== 사용 예 ==
1. Handler
public class HandlerEx extends Activity{
TextView tv = null;
ProgressBar pb = null;
ProgressBar pb2 = null;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.handler_ex);
tv = (TextView)findViewById(R.id.text);
pb = (ProgressBar)findViewById(R.id.pb);
pb.setBackGroundColor(Color.BLUE);
pb2 = (ProgressBar)findViewById(R.id.pb2);
//빈페이지 보내기
h.sendEmptyMessage(0);
}
//Handler 객체 생서
Handler h = new Handler(){
int i = 1;
public void handleMessage(Message m){
pb.incrementProgressBar(10);
tv.setText(i * 10) + “%”);
if( i <= 10 ){
//1초 지연 시간을 두어 자신에게 다시 메시지를 보낸다.
h.sendEmptyMessageDelayed(0, 1000);
} else {
tv.setText(“완료”);
}
i ++;
}
};
}
2. CountDownTimer
public class CountDownTimerEx extends Activity{
TextView tv = null;
ProgressBar pb = null;
ProgressBar pb2 = null;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.cdt_ex);
tv = (TextView)findViewById(R.id.text);
pb = (ProgressBar)findViewById(R.id.pb);
pb.setBackGroundColor(Color.BLUE);
pb2 = (ProgressBar)findViewById(R.id.pb2);
//CountDownTimer 를 이용한 방법
new CountDownTimer(1000 * 10, 1000){
int i = 1;
public void onTick(long m){
incrementBy();
}
public void onFinish(){
incrementBy();
tv.setText(“완료”);
}
public void incrementBy(){
tv.setText( i * 10 + “%”);
pb.incrementProgressBy(10);
i++;
}
}.start();
}
}