开发者

How to Cancel the TimerTask in BlackBerry

开发者 https://www.devze.com 2023-03-07 00:15 出处:网络
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

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. The run() 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:

public void start() {
    timer = new Timer();
    timer.schedule(this, 0, 150000);
}

Change your run() method to this:

public void run() {
    resetGPS();
}

Finally, in the class' constructor, call this.start() instead of this.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 boolean inside your Timer_Task class called _isRunning, and set this to true when you start the timer and set it to false inside your Stop method. You would then check this variable inside the run() method, and return if _isRunning is false (this lets you ignore any pending run() calls after you stop the timer).

0

精彩评论

暂无评论...
验证码 换一张
取 消