I am using the TimerTask in my app to update the current GPS to the server. I have extended the TimerTask and override the run method to do so. I have a stoptimer button in screen that should stop the timer, once it is clicked. but my problems is, even the timerobject.cancel()
is getting executed, but the timer is still running.
Can anybody share your opinion on stopping the timer when a particular button is clicked. Below is the code that i have written to run the timer task.
PointMyLocation.java
public class PointMyLocation
{
private String log;
double longi;
double lati;
public String email, city;
private HttpServiceCommunication mHttpService;
// Default Constructor
public PointMyLocation(int value){
new LocationTracker(value).StopTimer();
}
public PointMyLocation(String email, String city)
{
this.email = email;
this.city = city;
new LocationTracker();
//mHttpService = new HttpServiceCommunication();
}
public boolean onClose()
{
Application.getApplication().requestBackground();
return false;
}
class LocationTracker extends TimerTask {
private double longitude, latitude;
private Timer timer;
private LocationProvider provider;
private BeaconingBean mBb;
int mTimeinterval;
Criteria cr;
public LocationTracker() {
cr= new Criteria();
this.run(); // Calling the run
}
public void run() {
timer = new Timer();
resetGPS();
//mTimeinterval = mBb.getmTimeInterval();
//System.out.println("Time Interval :" + mTimeinterval);
timer.schedule(this, 0, 150000);
}
public void StopTimer(){
// Terminates the timer
this.timer.cancel(); // Though this statement gets executed, the timer starts again
}
public void resetGPS()
{
try
{
provider = LocationProvider.getInstance(cr);
if(provider != null)
{
provider.setLocationListener(new MyLocationListener(), 3, -1, -1);
}
} catch(Exception e){ }
}
private class MyLocationListener implements LocationListener
{
public void locationUpdated(LocationProvider provider, Location location)
{
if(location != null && location.isValid())
{
QualifiedCoordinates qc = location.getQualifiedCoordinates();
try
{
lati = location.getQualifiedCoordinates().getLatitude();
System.out.println("latitude :: "+lati);
longi = location.getQualifiedCoordinates().getLongitude();
System.out.println("longitude ::"+longi);
System.out.println("Email :: " + email);
System.out.println("City ::" + city);
}
catch(Exception e)
{
}
}
}
public void providerStateChanged(LocationProvider provider, int newState)
{
//LocationTracker.this.resetGPS();
if(newState == LocationProvider.TEMPORARILY_UNAVAILABLE)
{
provider.reset();
provider.setLocationListener(null, 0, 0, -1);
}
}
}
}
}
Any help is highly appreciated
Your basic problem is that you’re re-starting your timer inside the
run()method, which you shouldn’t be doing. Therun()method gets called every time the timer “ticks” – you don’t want to be altering and restarting your timer object in here.Try this (not tested but it should work). Add a method to your Timer_Task class called start:
Change your
run()method to this:Finally, in the class’ constructor, call
this.start()instead ofthis.run().The reason your stop method doesn’t appear to stop the timer is that even when you cancel a timer, if there’s one pending
run()call out there, the call will still happen even if the timer has been cancelled. When this last call happens in your existing code, a new timer is created and started, so the overall process never stops.Edit: One other change you should make is to add a
booleaninside your Timer_Task class called_isRunning, and set this totruewhen you start the timer and set it tofalseinside yourStopmethod. You would then check this variable inside therun()method, andreturnif_isRunningis false (this lets you ignore any pendingrun()calls after you stop the timer).