Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中经过一段时间后重新执行代码_Java_Android - Fatal编程技术网

Java 在android中经过一段时间后重新执行代码

Java 在android中经过一段时间后重新执行代码,java,android,Java,Android,我在谷歌地图项目中,以下是我在oncreate中的代码: mapView = (MapView)findViewById(R.id.mapView); mapView.setBuiltInZoomControls(true); mapView.setSatellite(false); mapView.setStreetView(true); mapController = mapView.getController();

我在谷歌地图项目中,以下是我在oncreate中的代码:

mapView = (MapView)findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(false);
        mapView.setStreetView(true);
        mapController = mapView.getController();
        mapController.setZoom(19);
        getLastLocation();
        drawCurrPositionOverlay();
        drawMalls();
        animateToCurrentLocation();

但是现在我想把这个叫做DrawMalls();方法,除非用户关闭此应用程序,否则在此时间之后将调用此方法?有什么方法可以做到这一点吗?

您可以使用
java.util.Timer
schedule()
方法来安排
drawMalls()的未来执行:

我不确定
drawMalls()
是静态方法还是非静态方法。如果它是静态的,那么调用
TimerTask.run()
方法就很简单了。否则,您需要安排
drawMalls()
所属的类实例可用于
TimerTask的
run()
方法:

class DrawMallsTask extends TimerTask
{
    public DrawMallsTask(YourClass a_build) { _instance = a_instance; }

    public void run() { _instance.DrawMalls(); }

    private YourClass _instance;
};

Timer t = new Timer();

t.schedule(new DrawMallsTask(this), 2000);
编辑:

要在每两秒钟后重复运行任务,可以使用:

t.scheduleAtFixedRate(new DrawMallsTask(this), 2000, 2000);

若重新执行的代码并没有绑定到应用程序的状态,而只是绑定到时间段,那个么请查看
Timer

您可以使用和组合在一段时间后执行语句

您可以使用Handler的方法延迟可运行

Runnable mRunnable;
Handler mHandler=new Handler();

mRunnable=new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                 drawMalls();
                 //If you want to re call this method at a gap of x seconds then you can schedule  handler again
                  mHandler.postDelayed(mRunnable,2*1000);       
                }
            };
mHandler.postDelayed(mRunnable,10*1000);//Execute after 10 Seconds
如果要取消此操作,则必须使用处理程序的方法,如
mHandler.removeCallbacks(mRunnable)


或者你可以使用定时器。您可以在这里引用一个示例,您可以按照使用ScheduledExecutorService的说明进行操作。我以前遇到过一些错误,在2.1中,计时器无法正常停止和启动。不过,所描述的调度方案对我来说非常有效。

有两种方法

1) 使用处理程序 2) 使用定时器

      //using Timer//
    public void OnCreate(Bundle SaveInstanceState())
    {
      ------------
      -----------------
      PreferedTime  pTime=new preferedTime();

      Timer t=new Timer(false);
      t.Schedule(pTime,2000);
   }


   class PreferedTime extends TimerTask
   {
      public void run()
      {
         drawMalls();
       }
   }




     //method 2//
     public void OnCreate(Bundle SaveInstanceState())
    {
      -----------------
      -----------------
     Handler handler=new handler(new Runnable()
     {
        public void run()
        {
            drawMalls();
         }
      },2000);

此函数每2秒调用一次drawMalls()。您可以根据需要进行更改。

可能有一些库方法,但您可以使用System.currenttimeMillis()手动执行此操作。如何执行此操作?你能给我举个例子吗?你的代码运行得很好,但是我想每两秒钟运行一次这个代码,然后怎么办?非常感谢,它正在工作,你能帮我再做一个吗?在这段代码中,我每隔2秒就重新绘制一些点,但是我想删除前面的点,我该怎么做?没问题。如果这解决了当前的问题,Sabbir你应该接受这个答案(只有在没有其他你喜欢的答案的情况下)并用新问题打开一个新问题:更多的人会看到它并能够提供帮助。当应用程序在到达超时之前停止时,你必须阻止执行此任务。
Runnable mRunnable;
Handler mHandler=new Handler();

mRunnable=new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                 drawMalls();
                 //If you want to re call this method at a gap of x seconds then you can schedule  handler again
                  mHandler.postDelayed(mRunnable,2*1000);       
                }
            };
mHandler.postDelayed(mRunnable,10*1000);//Execute after 10 Seconds
      //using Timer//
    public void OnCreate(Bundle SaveInstanceState())
    {
      ------------
      -----------------
      PreferedTime  pTime=new preferedTime();

      Timer t=new Timer(false);
      t.Schedule(pTime,2000);
   }


   class PreferedTime extends TimerTask
   {
      public void run()
      {
         drawMalls();
       }
   }




     //method 2//
     public void OnCreate(Bundle SaveInstanceState())
    {
      -----------------
      -----------------
     Handler handler=new handler(new Runnable()
     {
        public void run()
        {
            drawMalls();
         }
      },2000);
MyCount counter;
@Override 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 counter= new MyCount(60000,1000);
counter.start();
}    


public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
 counter= new MyCount(60000,1000);
}
@Override
public void onTick(long millisUntilFinished) {
    s1=millisUntilFinished/1000;
 if(s1%2==0)
{
drawMalls();

}


}
}