1

我在我的应用程序中使用 TimerTask 将当前 GPS 更新到服务器。我已经扩展了 TimerTask 并重写了 run 方法来做到这一点。我在屏幕上有一个停止计时器按钮,一旦单击它就应该停止计时器。但我的问题是,即使timerobject.cancel() 正在执行,但计时器仍在运行。

任何人都可以分享您对单击特定按钮时停止计时器的看法。下面是我为运行计时器任务而编写的代码。

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);
            }
    }
}
} 
}

非常感谢任何帮助

4

1 回答 1

2

您的基本问题是您在run()方法内重新启动计时器,您不应该这样做。每次计时器“滴答”时都会调用该run()方法-您不想在这里更改和重新启动计时器对象。

试试这个(未经测试,但它应该工作)。向 Timer_Task 类添加一个名为 start 的方法:

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

run()将您的方法更改为:

public void run() {
    resetGPS();
}

最后,在类的构造函数中,调用this.start()而不是this.run().

您的 stop 方法似乎没有停止计时器的原因是,即使您取消计时器,如果那里有一个待处理run()的呼叫,即使计时器已被取消,呼叫仍会发生。当您的现有代码中发生最后一次调用时,会创建并启动一个新计时器,因此整个过程永远不会停止。

编辑: 您应该进行的另一项更改是boolean在您的 Timer_Task 类中添加一个名为_isRunning,并将其设置为true当您启动计时器并将其设置为false您的Stop方法中。然后,您将在run()方法内检查此变量,return如果为 false(这使您可以在停止计时器后_isRunning忽略任何挂起的调用)。run()

于 2011-05-18T15:15:51.203 回答