簡體   English   中英

一定時間后在android中重新執行代碼

[英]Re executing a code after a certain period of time in android

我在一個谷歌地圖項目中,這是我在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(); 幾秒鍾后調用此方法,除非用戶關閉此應用程序,否則將在該時間之后調用此方法? 有什么辦法嗎?

您可以在一段時間后使用HandlerRunnable組合來執行語句。

您可以使用Handler的postDelayed()方法來延遲Runnable。

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

如果要取消此操作,則必須使用Handler的removeCallback()方法,例如mHandler.removeCallbacks(mRunnable);

或者您可以使用計時器。 您可以在此處參考示例http://thedevelopersinfo.wordpress.com/2009/10/18/scheduling-a-timer-task-to-run-repeatedly/

您可以使用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);

您可以按照此處使用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);
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()。.u可以根據需要更改它。

如果重新執行的代碼不綁定到應用程序狀態,而僅綁定到時間段,請查看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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM