【问题标题】:How to Cancel the TimerTask in BlackBerry如何在 BlackBerry 中取消 TimerTask
【发布时间】:2011-05-18 15:04:10
【问题描述】:

我在我的应用程序中使用 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);
            }
    }
}
} 
}

非常感谢任何帮助

【问题讨论】:

    标签: blackberry timertask


    【解决方案1】:

    您的基本问题是您在 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() 呼叫,即使计时器已被取消,呼叫仍会发生。当您的现有代码中发生最后一次调用时,会创建并启动一个新计时器,因此整个过程永远不会停止。

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

    【讨论】:

    • 嗨 Gene,感谢您的回复,我会尝试并让您知道。非常感谢朋友。
    • 嗨 Gene,我按照你的方法试过了,但我仍然无法停止正在运行的计时器。如果您找到更好的解决方案,请告诉我。谢谢……
    猜你喜欢
    • 2010-12-23
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多