Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / viewmodels / TimerViewModel.java
  1. package com.example.nemophila.viewmodels;
  2.  
  3. import com.example.nemophila.Nemophila;
  4.  
  5. import java.util.concurrent.ScheduledThreadPoolExecutor;
  6. import java.util.concurrent.TimeUnit;
  7.  
  8. import androidx.lifecycle.ViewModel;
  9.  
  10. abstract class TimerViewModel extends ViewModel implements Runnable {
  11. private ScheduledThreadPoolExecutor thread = null;
  12. protected Nemophila nemophila;
  13.  
  14.  
  15. //-----------------------------------------------------------------
  16. // 一定間隔で呼び出す
  17. @Override
  18. public void run() {
  19. update();
  20. }
  21.  
  22. public abstract void update();
  23.  
  24. //-----------------------------------------------------------------
  25. //何ミリ秒ごとにrun()を実行するかを決める
  26. public void start(int interval, Nemophila nemophila) {
  27. this.nemophila = nemophila;
  28. thread = new ScheduledThreadPoolExecutor(1);
  29. thread.scheduleWithFixedDelay(this, interval, 1000L, TimeUnit.MILLISECONDS);
  30. }
  31.  
  32. //-----------------------------------------------------------------
  33. //
  34. public void stop() {
  35. thread.shutdown();
  36. }
  37. //-----------------------------------------------------------------
  38. }