ScheduledFuture とScheduledExecutorService
http://ameblo.jp/vegetable-it/entry-11245144566.html
Androidで一時停止はonPause()、再会するときはonRestart()を呼ぶ。(onResume()だとうまくいかなかった)
package com.example.timertest; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { HogeTimerTask timerTask; ScheduledFuture future; ScheduledExecutorService scheduler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timerTask = new HogeTimerTask(); scheduler = Executors.newSingleThreadScheduledExecutor(); future = scheduler.scheduleAtFixedRate(timerTask, 0, 1000, TimeUnit.MILLISECONDS); } class HogeTimerTask implements Runnable { @Override public void run() { Log.e("TAG", "hogehoge timer task activated."); } } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.e("STOP","Timer schedule stopped."); future.cancel(true); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); future = scheduler.scheduleAtFixedRate(timerTask, 0, 1000, TimeUnit.MILLISECONDS); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }