Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / viewmodels / TimerViewModel.java
package com.example.nemophila.viewmodels;

import com.example.nemophila.Nemophila;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import androidx.lifecycle.ViewModel;

abstract class TimerViewModel extends ViewModel implements  Runnable {
    private ScheduledThreadPoolExecutor thread = null;
    protected Nemophila nemophila;


    //-----------------------------------------------------------------
    // 一定間隔で呼び出す
    @Override
    public void run() {
        update();
    }

    public abstract void update();

    //-----------------------------------------------------------------
    //何ミリ秒ごとにrun()を実行するかを決める
    public void start(int interval, Nemophila nemophila) {
        this.nemophila = nemophila;
        thread = new ScheduledThreadPoolExecutor(1);
        thread.scheduleWithFixedDelay(this, interval, 1000L, TimeUnit.MILLISECONDS);
    }

    //-----------------------------------------------------------------
    //
    public void stop() {
        thread.shutdown();
    }
    //-----------------------------------------------------------------
}