1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | public class MainActivity extends Activity { private TextView timeTv; private Timer mTimer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timeTv = (TextView) findViewById(R.id.timeTv); MainTimerTask timerTask = new MainTimerTask(); mTimer = new Timer(); mTimer.schedule(timerTask, 500, 1000); } private Handler mHandler = new Handler(); private Runnable mUpdateTimeTask = new Runnable() { public void run() { Date rightNow = new Date(); SimpleDateFormat formatter = new SimpleDateFormat( "yyyy.MM.dd hh:mm:ss "); String dateString = formatter.format(rightNow); timeTv.setText(dateString); } }; class MainTimerTask extends TimerTask { public void run() { mHandler.post(mUpdateTimeTask); } } @Override protected void onDestroy() { mTimer.cancel(); super.onDestroy(); } @Override protected void onPause() { mTimer.cancel(); super.onPause(); } @Override protected void onResume() { MainTimerTask timerTask = new MainTimerTask(); mTimer.schedule(timerTask, 500, 3000); super.onResume(); } } | cs |
'이것저것 > 자바*안드로이드' 카테고리의 다른 글
안드로이드 진동모드(무음,벨소리) 확인하기 (0) | 2017.01.03 |
---|---|
안드로이드 기기의 핸드폰 번호 가져오기 (0) | 2016.12.29 |
안드로이드 스튜디오 jar 파일 추가 (0) | 2016.12.07 |
TabWidget 탭 이미지 변경 (0) | 2016.11.28 |
xml 파싱 (0) | 2016.11.24 |