如何取消BlackBerry中的TimerTask
我正在使用应用程序中的TimerTask将当前GPS更新到服务器。我已经扩展了TimerTask,并重写了run方法来实现这一点。我在屏幕上有一个stoptimer按钮,一旦点击它,它就会停止计时器。但我的问题是,即使是如何取消BlackBerry中的TimerTask,blackberry,timertask,Blackberry,Timertask,我正在使用应用程序中的TimerTask将当前GPS更新到服务器。我已经扩展了TimerTask,并重写了run方法来实现这一点。我在屏幕上有一个stoptimer按钮,一旦点击它,它就会停止计时器。但我的问题是,即使是timerobject.cancel() 正在执行,但计时器仍在运行 任何人都可以分享你的意见停止计时器时,一个特定的按钮被点击。下面是我为运行计时器任务而编写的代码 PointMyLocation.java public class PointMyLocation { p
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);
}
}
}
}
}
非常感谢您提供的任何帮助您的基本问题是在
run()
方法中启动计时器,这是您不应该做的。每次计时器“滴答”时都会调用run()
方法-您不希望在此处更改和重新启动计时器对象
试试这个(未经测试,但它应该可以工作)。向计时器任务类添加名为start的方法:
public void start() {
timer = new Timer();
timer.schedule(this, 0, 150000);
}
将run()
方法更改为:
public void run() {
resetGPS();
}
最后,在类的构造函数中,调用this.start()
,而不是this.run()
stop方法似乎没有停止计时器的原因是,即使在取消计时器时,如果有一个挂起的run()
调用,即使计时器已取消,调用仍会发生。当在现有代码中发生最后一个调用时,将创建并启动一个新的计时器,因此整个过程永远不会停止
Edit:您应该做的另一个更改是在名为
\u isRunning
的Timer\u任务类中添加boolean
,并在启动计时器并在Stop
方法中将其设置为true
时将其设置为false
。然后,您将在run()
方法中检查此变量,如果\u isRunning
为false,则返回(这允许您在停止计时器后忽略任何挂起的run()
调用)。Hi Gene,感谢您的响应,我将尝试使用它并让您知道。非常感谢,朋友。嗨,吉恩,我试过用你的方式,但仍然无法停止正在运行的计时器。如果你有更好的解决办法,请告诉我。非常感谢。