2

This is the problem about loading. My application should wait for 3 seconds and then draw path. Now, the loading message box appears within 3 seconds but the application does not wait for 3 seconds and draw immediately . Is there any problem in my coding ?

Many Thanks!!

 public void drawpath(){
       // first to do some checking
    if (sourceLat.equals("22.3366467")  && destinationLat.equals("35.68449"))
        ShowMsgDialog("Please enter the starting point and destination");
    else if (sourceLat.equals("22.3366467") )
        ShowMsgDialog("Please enter the starting point by touch");
    else if (destinationLat.equals("35.68449") )
        ShowMsgDialog("Please enter the destination by touch");

    else if (pairs != null ){
        //  go to loading function
        loading();
        // Start to draw the path
        String[] lngLat = pairs[0].split(",");

        GeoPoint startGP = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double.parseDouble(lngLat[0]) * 1E6));

         mc = mapView.getController();
         geoPoint = startGP;
         mc.setCenter(geoPoint);
         mc.setZoom(15);
         mapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));

                ...... 
        }

in the loading() function:

private void loading() {
    progressDialog = ProgressDialog.show(this, "Showing Data..", "please wait", true, false);
    new Thread()
    { 
      public void run()
      { 
        try{ 
          sleep(3000);
        }
        catch (Exception e){
          e.printStackTrace();
        }
        finally{
            progressDialog.dismiss();    
        }
      }
    }.start(); 
}
4

4 回答 4

25

您也可以通过以下方式执行此操作:

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            drawPath();
        }
    }, 5000);

这不会显示任何 lint 警告。

于 2012-11-29T12:12:30.853 回答
2

处理程序会很容易地完成这项工作。您不需要单独的线程或 AsyncTask()。

在您的 Activity 中使用 Handler 来延迟事件,例如在您的情况下调用方法 drawPath() :

private RefreshHandler mRedrawHandler = new RefreshHandler(); 
 private RefreshHandler mRedrawHandler = new RefreshHandler(); 

     class RefreshHandler extends Handler {  
            @Override  
            public void handleMessage(Message msg) {  
          drawPath(); 
            }  

            public void sleep(long delayMillis) {  
              this.removeMessages(0);  
              sendMessageDelayed(obtainMessage(0), delayMillis);  
            }  
          };  

在您的 Activity 的 onCreate() 中(或在任何按钮的 onClick() 中,只要您想开始延迟), callmRedrawHandler.sleep(3000); , drawPath();是您开始绘制的方法。

于 2012-04-07T15:18:11.920 回答
0

生成新线程后,您的代码将重新获得控制权并继续执行。您的附加线程正在等待不是原始线程。

于 2012-04-07T13:33:38.913 回答
0

用于 android 活动中的延迟器

      import android.os.Handler;

在代码主

  new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
  
        }
    }, 5000);

但是上面的代码已经过时了

最好使用以下代码

将其用于 Java

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
    @Override
    public void run() {
        // Your Code
    }
}, 3000);

将它用于 Kotlin

Handler(Looper.getMainLooper()).postDelayed({
        {
            //Your Code
        }
    }, 3000)
于 2020-11-19T07:41:02.940 回答