必要なファイルを 15 分ごとにチェックできる Android サービスを作成したいと考えています。
そのために、TTS を使用してテキストを 10 10 秒ごとに再生するサンプル プログラムを作成しました。また、アラーム マネージャーを使用して 30 秒ごとにサービスを呼び出しました。
サービスは完全に呼び出され、TTS も初めて完全に再生されますが、サービスが 50 秒後に再度呼び出されると、タイマーは 0 から開始されず、代わりに 11、12、13 から開始されます - cancel() を指定したにもかかわらず.
これを解決する方法について誰かが私を助けることができますか?
以下はコードです:
public class ServiceLocation extends Service implements OnInitListener
{
TextToSpeech talker;
Timer t;
public int time = 0;
@Override
public void onCreate()
{
super.onCreate();
Log.e("Location1", "Inside onCreate");
}
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
t = new Timer();
Log.e("Location1", "Inside onStart");
talker = new TextToSpeech(this, this);
testMethod();
}
public void testMethod()
{
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
time += 1;
String todis = String.valueOf(time);
if(todis.contains("20"))
{
talker.speak("Testing Service in a App",TextToSpeech.QUEUE_ADD,null);
t.cancel();
t.purge();
}
}
}, 0, 1000);
}
public void onInit(int status)
{
talker.speak("Testing Service in a App",TextToSpeech.QUEUE_ADD,null);
}
}