I want to create an android service that can check on required files every 15 mins.
For which, I have created a sample program that plays a text using TTS every ten 10 seconds. And also used a alarm manager to call the service every 30 seconds
The service is call perfectly and even the TTS is played perfectly the first time but when the service is called again after 50 seconds, the timer is not starting from 0 instead starts from 11, 12, 13 - even though I have given cancel().
Can some body help me out on how to solve this?
Below are the code:
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);
}
}