【发布时间】:2013-04-20 06:45:15
【问题描述】:
我想创建一个可以每 15 分钟检查一次所需文件的 android 服务。
为此,我创建了一个示例程序,它使用 TTS 每 10 秒播放一次文本。并且还使用了一个警报管理器,每 30 秒调用一次服务
服务被完美调用,甚至第一次完美播放 TTS 但是当服务在 50 秒后再次被调用时,计时器不是从 0 开始而是从 11、12、13 开始 - 即使我已经给出取消()。
有人可以帮我解决这个问题吗?
下面是代码:
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);
}
}
【问题讨论】: