[Andriod] 현재 시간 얻기 소스
2012. 1. 2. 14:48ㆍ개발관련기록/Android(인조인간)
반응형
001 |
package com.Second_Test; |
002 |
003 |
import java.util.Calendar; |
004 |
import java.util.Date; |
005 |
import java.util.Timer; |
006 |
import java.util.TimerTask; |
007 |
import android.app.Activity; |
008 |
import android.os.Bundle; |
009 |
import android.os.Handler; |
010 |
import android.util.Log; |
011 |
import android.view.View; |
012 |
import android.widget.Button; |
013 |
import android.widget.TextView; |
014 |
015 |
public class Second_Test extends Activity |
016 |
{ |
017 |
int curYear, curMonth, curDay, curHour, curMinute, curNoon, curSecond; |
018 |
|
019 |
Calendar c; |
020 |
|
021 |
String tag = null ; |
022 |
String noon = "" ; |
023 |
024 |
Date curMillis; |
025 |
026 |
TimerTask second; |
027 |
028 |
TextView getData; |
029 |
|
030 |
|
031 |
private final Handler handler = new Handler(); |
032 |
033 |
@Override |
034 |
public void onCreate(Bundle savedInstanceState) |
035 |
{ |
036 |
super .onCreate(savedInstanceState); |
037 |
setContentView(R.layout.main); |
038 |
039 |
Button bt1 = (Button)findViewById(R.id.Button01); |
040 |
bt1.setText( "현재 날짜와 시간 구하기" ); |
041 |
bt1.setOnClickListener( new View.OnClickListener() |
042 |
{ |
043 |
public void onClick(View v) |
044 |
{ |
045 |
Test(); |
046 |
} |
047 |
}); |
048 |
049 |
|
050 |
051 |
} |
052 |
053 |
054 |
private void Test() |
055 |
{ |
056 |
getData = (TextView)findViewById(R.id.TextView01); |
057 |
058 |
second = new TimerTask() |
059 |
{ |
060 |
private String tag; |
061 |
062 |
@Override |
063 |
public void run() |
064 |
{ |
065 |
Log.d(tag, curYear+ "." +curMonth+ "." +curDay+ "." +curHour+ ":" +curMinute+ "." +curSecond); |
066 |
Update(); |
067 |
} |
068 |
}; |
069 |
Timer timer = new Timer(); |
070 |
timer.schedule(second, 0 , 1000 ); |
071 |
072 |
} |
073 |
074 |
075 |
protected void Update() |
076 |
{ |
077 |
c = Calendar.getInstance(); |
078 |
curMillis = c.getTime(); |
079 |
curYear = c.get(Calendar.YEAR); |
080 |
curMonth = c.get(Calendar.MONTH)+ 1 ; |
081 |
curDay = c.get(Calendar.DAY_OF_MONTH); |
082 |
curHour = c.get(Calendar.HOUR_OF_DAY); |
083 |
curNoon = c.get(Calendar.AM_PM); |
084 |
if (curNoon == 0 ) |
085 |
{ |
086 |
noon = "오전" ; |
087 |
} |
088 |
else |
089 |
{ |
090 |
noon = "오후" ; |
091 |
curHour -= 12 ; |
092 |
} |
093 |
curMinute = c.get(Calendar.MINUTE); |
094 |
curSecond = c.get(Calendar.SECOND); |
095 |
|
096 |
|
097 |
Runnable updater = new Runnable() |
098 |
{ |
099 |
public void run() |
100 |
{ |
101 |
getData.setText( "현재 날짜와 시간은 " +curYear+ "년 " +curMonth+ "월 " +curDay+ "일 " + |
102 |
noon+curHour+ "시 " +curMinute+ "분 " +curSecond+ "초 입니다. " ); |
103 |
|
104 |
} |
105 |
}; |
106 |
handler.post(updater); |
107 |
} |
108 |
} |
main.xml 내용
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Time">
</Button>
<TextView
android:text="@+id/TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
레이아웃의 구성은 간략하다.
버튼하나, 텍스트뷰 끗. -_-;
본문 소스 중간에 보면... 로그캣을 남겨두었기 때문에 디버깅 모드로 DDMS를 관찰해보면 실제 시간을 볼 수 있다.
( ↑↑↑ 이곳의 시간은 실제 시간 ↑↑↑↑ 이곳의 시간이 log.d로 남긴 시간.)
1초의 오차가 있다. -_ㅜ;
실제 갤럭시S에서 어플을 구동해보았다.
버튼을 누르면 현재 날짜와 시간이 1초 간격으로 refresh 되면서 출력된다.
1초마다 쓰레드를 동작시키는 TimerTask 및 Timer 스케쥴러 메서드와.
이를 업데이트 시켜주는 handler의 기능이 어우러진 합작품이었다. ㅎ
근데 오늘 날씨 완전 덥네 ㅜ윽.
반응형
'개발관련기록 > Android(인조인간)' 카테고리의 다른 글
[Android] 안드로이드에서 DataBase 관리 , DB관리 (0) | 2012.01.05 |
---|---|
[Android] Tab 이미지 배경색 넣기, 아이콘 생성하기. (1) | 2012.01.03 |
[Android 오류] Error in an XML file: aborting build. 오류 (0) | 2011.12.30 |
[Android] 물리엔진관련 내용 [펌] (0) | 2011.12.06 |
[Android] Sample Source (0) | 2011.12.06 |