2017-03-09 33 views
0
final int delayTime = 20000; 

    final Handler handler = new Handler(); 

    Runnable runnable = new Runnable() { 
     @Override 
     public void run() { 
      String Refresh_Location; 
      Refresh_Location = "http://maps.google.com/maps?q=loc:" + "" + currentLatitude + "," + currentLongitude; 
      Log.e("Refresh Location", "--->" + Refresh_Location); 

      new SendLiveLatLong().execute(); 
      handler.postDelayed(this, delayTime); 
     } 
    }; 

    handler.postDelayed(runnable, delayTime); 

我用这个函数来调用Web服务我的每20秒。但它不会正常工作,并且不会每次都调用该Web服务。处理函数不会调用web服务

+0

它应该在20秒后只执行一次。你做了什么“每20秒”执行一次?发布完整的代码。 – tahsinRupam

+0

我想调用新的SendLiveLatLong()。execute();每20秒后。这是我的web服务。 –

+0

后SendLiveLatLong代码 – shadygoneinsane

回答

1

我有这个问题,并用Timer替换Handler,并使用TimerTask代替runnable。 现在一切都很好。

+0

我也试过它与计时器。定时器计时器=新的计时器(); timer.schedule(新的TimerTask(){ @Override 公共无效的run(){ 字符串Refresh_Location; Refresh_Location = “http://maps.google.com/maps?q=loc:” + “” + currentLatitude + “” + currentLongitude; Log.e( “刷新位置”, “--->” + Refresh_Location); 新SendLiveLatLong()执行(); } },20000); 但它仍然没有工作。 –

+0

使用timer.schedule(新的TimerTask(......),20000,20000); –

+0

实际上,它每隔20秒后都能正常工作并显示日志,但不会每次都调用该web服务。 –