有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在安卓中经过一段时间后重新执行代码

我在谷歌地图项目中,以下是我在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();方法,除非用户关闭此应用程序,否则在此时间之后将调用此方法?有没有办法做到这一点


共 (6) 个答案

  1. # 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. # 2 楼答案

    您可以按照使用ScheduledExecutorService here的说明进行操作。我以前遇到过一些错误,在2.1版本中,计时器无法正常停止和启动,但所描述的调度方案对我来说非常有效

  3. # 3 楼答案

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

    这个函数每2秒调用一次drawMalls()。。你可以根据需要更改它

  4. # 4 楼答案

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

    http://developer.android.com/reference/java/util/Timer.html

    Timer timer;
    
    function myCallerFunction(){
        timer = new Timer();
        timer.schedule(seconds * 1000); //must be in milliseconds
    }
    
    private class MyTask extends TimerTask {
        public void run() {
          drawMalls();
        }
    }
    
  5. # 5 楼答案

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

    Timer t = new Timer();
    
    t.schedule(
        new TimerTask()
        {
            public void run()
            {
                System.out.println("hello\n");
            }
        },
        2000); // Milliseconds: 2 * 1000
    

    我不确定drawMalls()static还是非静态方法。如果是static,则直接调用TimerTask.run()方法。否则,您需要安排drawMalls()所属的类实例可用于TimerTaskrun()方法:

    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);
    
  6. # 6 楼答案

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

    您可以使用处理程序的postDelayed()方法延迟可运行

    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);的处理程序的removeCallback()方法

    或者你可以使用定时器。你可以在这里引用一个例子http://thedevelopersinfo.wordpress.com/2009/10/18/scheduling-a-timer-task-to-run-repeatedly/