안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요! 다름이 아니라
실시간으로 시간을 받아오려면
쓰레드나 타이머를 이용해서 계속확인하고 받아오는 방법뿐인가요?ㅜㅜ
관련된 메소드 아시면 알려주세요! 감
2013.02.07 15:33:31
public class BaseClock extends DigitalClock {
private final static String mFormat = "h:mm";
Calendar mCalendar;
private Runnable mTicker;
private Handler mHandler;
private boolean mTickerStopped = false;
public BaseClock(Context context) {
super(context);
initClock(context);
}
public BaseClock(Context context, AttributeSet attrs) {
super(context, attrs);
initClock(context);
}
private void initClock(Context context) {
if (mCalendar == null) {
mCalendar = Calendar.getInstance();
}
}
@Override
protected void onAttachedToWindow() {
mTickerStopped = false;
super.onAttachedToWindow();
mHandler = new Handler();
/**
* requests a tick on the next hard-second boundary
*/
mTicker = new Runnable() {
public void run() {
if (mTickerStopped)
return;
mCalendar.setTimeInMillis(System.currentTimeMillis());
setText(DateFormat.format(mFormat, mCalendar) + (mCalendar.get(Calendar.AM_PM) == 0 ? " AM" : " PM"));
invalidate();
long now = SystemClock.uptimeMillis();
long next = now + (1000 - now % 1000);
mHandler.postAtTime(mTicker, next);
}
};
mTicker.run();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mTickerStopped = true;
}
}



