取消了Runnable在LocationOverlay对象的.runOnFirstFix()方法对象、方法、取消了、LocationOverlay

由网友(魅寒~)分享简介:我有地图功能的应用程序的大量斜靠。从第一次活动我称之为 runOnFirstFix()方法,一旦用户的位置已经发现,从数据库中装载了大量的数据,但我也希望能够打断这个可运行和停止执行中期,当我切换活动或用户presses按钮停止运行它。I have an application the leans heavily o...

我有地图功能的应用程序的大量斜靠。从第一次活动我称之为 runOnFirstFix()方法,一旦用户的位置已经发现,从数据库中装载了大量的数据,但我也希望能够打断这个可运行和停止执行中期,当我切换活动或用户presses按钮停止运行它。

I have an application the leans heavily on map functionality. From the first Activity I call the runOnFirstFix() method to load a lot of data from a database once the location of the user has been found, but I also want to be able to interrupt this runnable and stop it mid execution for when I switch activity or the user presses the button to stop it running.

myLocationOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                mc.animateTo(myLocationOverlay.getMyLocation());
                mc.setZoom(15);
                userLatitude = myLocationOverlay.getMyLocation().getLatitudeE6();
                userLongitude = myLocationOverlay.getMyLocation().getLongitudeE6();
                userLocationAcquired = true;
                loadMapData();  //Here the method is called for heavy data retrieval    
            }
        });

我怎样才能阻止这种Runnable的执行中期?

How can I stop this Runnable mid execution?

推荐答案

使用handler对象来处理这个运行的。

use the handler object to handle this runnable.

定义这个可运行在Runnable对象。

define this runnable with the runnable object.

之后,在处理程序即可启动取消该服务,可运行

after that in handler you can start the cancel this runnable service

有关如

Handler handler = new Handler();

在启动命令()

handler.postDelayed(myRunnable,5000);

这将在5秒之后执行的可运行的run方法

this will execute the run method of runnable after 5 sec

有关取消

handler.removeCallbacks(myRunnable);

和您可运行这样定义的方式

and your runnable define like this way

private Runnable myRunnable = new Runnable(){
      public void run(){
          // do something here
      }
}

http://developer.android.com/reference/android/os/ Handler.html

http://developer.android.com/reference/java/ UTIL /日志/ Handler.html

http://www.vogella.de/articles/AndroidPerformance/article.html

阅读全文

相关推荐

最新文章