我想创建一个可以每15分钟检查一次所需文件的安卓服务。Android服务中的计时器
为此,我创建了一个示例程序,每10秒钟使用TTS播放一个文本。并且还使用警报管理器每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);
}
}