2012-02-23 6 views
4

私はアプリケーションを終了するアプリケーションをシャットダウンし、ハンドラで実装された遅延実行可能ファイルを終了します。ユーザーがタイマーアイコンをクリックすると、残っている時間も表示されます。このデータはどうすれば入手できますか? 秒をカウントしてそのデータを使用するオブジェクトを実装する必要がありますか?Android:ハンドラ経由でRunnableが実行されるまでの時間を取得

答えて

4

私は、ScheduledFutureでこれらのAPIをScheduledExecutorServiceを使用することを好むは、IMOハンドラーとタイマーよりも効率的かつ強力である:

ScheduledExecutorService scheduledTaskExecutor = Executors.newScheduledThreadPool(1); 
Runnable quitTask = new Runnable(); 

// schedule quit task in 2 minutes: 
ScheduledFuture scheduleFuture = scheduledTaskExecutor.schedule(quitTask, 2, TimeUnit.MINUTES); 

... ... 
// At some point in the future, if you want to get how much time left: 
long timeLeft = scheduleFuture.getDelay(TimeUnit.MINUTES); 
... ... 

お役に立てば幸いです。

+0

ありがとうございます、これは仕事を削減します。 –

+0

['TimeUnit.MINUTES'はAndroidに存在することが保証されていません](http://stackoverflow.com/questions/3472423/android-java-util-concurrent-timeunit-convert-milliseconds-to-minutes) –

関連する問題