如何延迟代码执行(绘制路径)几秒钟?

Yen*_*eny 2 android handler progressdialog delayed-execution

这是关于加载的问题.我的应用程序应该等待3秒然后绘制路径.现在,加载消息框在3秒内出现,但应用程序不会等待3秒并立即绘制.我的编码有问题吗?

非常感谢!!

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

                ...... 
        }
Run Code Online (Sandbox Code Playgroud)

在loading()函数中:

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(); 
}
Run Code Online (Sandbox Code Playgroud)

小智 25

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

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            drawPath();
        }
    }, 5000);
Run Code Online (Sandbox Code Playgroud)

这不会显示任何lint警告.