2012. 8. 23. 23:35
안드로이드 - 타이머 기능 안드로이드 이야기2012. 8. 23. 23:35
728x90
반응형
안드로이드 - 타이머 기능

2011/03/25 01:00
http://blog.naver.com/kjs077/10105694192
출저: http://www.gisdeveloper.co.kr/623
- package mobile.geoservice;
- import android.app.*;
- import android.os.*;
- import android.view.*;
- import android.widget.*;
- public class Timer extends Activity {
- private TextView _text;
- private CountDownTimer _timer;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.timerlayout);
- _text = (TextView)findViewById(R.id.tvMsg);
- _timer = new CountDownTimer(10 * 1000, 1000) {
- public void onTick(long millisUntilFinished) {
- _text.setText("value = " + millisUntilFinished);
- }
- public void onFinish() {
- _text.setText("finshed");
- }
- };
- Button btnStart = (Button)findViewById(R.id.btnStart);
- btnStart.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- _timer.start();
- }
- });
- Button btnEnd = (Button)findViewById(R.id.btnStop);
- btnEnd.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- _timer.cancel();
- }
- });
- }
- }
이렇게 만든 타이머의 start 매서드와 cancel 매서드를 통해 시작시키거나 작동을 중지시킬 수 있습니다. cancel 매서드의 경우 여타 다른 환경의 타이머와 다르게 취소만 될뿐.. 중지하여 중지된 시점으로부터 재개할 수는 없습니다. 또한 확인해 본바로는 cancel 매서드를 onTick 매서드 안에서 호출할 경우 의도와 다르게 타이머가 중지하지 않습니다..
이해를 돕고자 위의 코드를 실행했을 경우 애뮬레이터에서 나타나는 UI는 아래 그림과 같습니다.

모바일에서 타이머의 기능을 어디에 활용할 수 있을까... 한번 생각을 해보면... 일정한 시간 간격으로 자신의 위치를 얻어오거나... 일정한 시간 간격으로 메일서버로부터 메일을 확인한다거나... 오히려 일반 데스크탑 환경에서보다 모바일에서 타이머의 기능은 매우 중요할듯합니다..
아뿔싸~ 레이아웃 리소스가 빠졌군요~! 실습을 하시는 분이라면 timerlayout.xml이라는 파일로해서 저장해주시면 됩니다.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
- a:orientation="vertical"
- a:layout_width="fill_parent"
- a:layout_height="fill_parent"
- a:gravity="center">
- <TextView
- a:id="@+id/tvMsg"
- a:layout_width="fill_parent"
- a:layout_height="wrap_content"
- a:text="타이머 정보"
- a:gravity="center"
- />
- <Button
- a:id="@+id/btnStart"
- a:layout_width="wrap_content"
- a:layout_height="wrap_content"
- a:text="start Timer"
- />
- <Button
- a:id="@+id/btnStop"
- a:layout_width="wrap_content"
- a:layout_height="wrap_content"
- a:text="stop Timer"
- />
- </LinearLayout>
[출처] 안드로이드 - 타이머 기능 |작성자 끙애내꼬
728x90
반응형
'안드로이드 이야기' 카테고리의 다른 글
안드로이드 - 안드로이드 개발팁 총정리 (0) | 2012.08.24 |
---|---|
안드로이드 - 움직이는 이미지 만들기 (0) | 2012.08.24 |
안드로이드 - jPCT-AE로 (근사) 직교투영(Orthogonal Projection) 실현하기 (0) | 2012.08.23 |
안드로이드 - 음성 인식 소스 (0) | 2012.08.23 |
안드로이드 actionbar 소스 (0) | 2012.08.23 |