【问题标题】:Handler function doesn't call the webservices处理程序函数不调用 web 服务
【发布时间】:2017-03-09 09:18:25
【问题描述】:
    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);

我使用此函数每 20 秒调用一次我的网络服务。但它不会正常工作,也不会每次都调用那个 web 服务。

【问题讨论】:

  • 它应该只在 20 秒后执行一次。你做了什么来执行“每” 20 秒?发布完整代码。
  • 我想调用 new SendLiveLatLong().execute();每 20 秒后。这是我的网络服务。
  • 发布 SendLiveLatLong 代码

标签: android web-services android-handler


【解决方案1】:

我遇到了这个问题,将 Handler 替换为 Timer 并使用 TimerTask 而不是 runnable。 现在一切都很好。

【讨论】:

  • 我也用计时器试过了。定时器 timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { String Refresh_Location; Refresh_Location = "maps.google.com/maps?q=loc:" + "" + currentLatitude + "," + currentLongitude; Log.e("刷新位置", " --->" + Refresh_Location); new SendLiveLatLong().execute(); } }, 20000);但它仍然没有工作。
  • 实际上它在每 20 秒后正常工作并显示日志,但不会每次都调用该 Web 服务。
猜你喜欢
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
相关资源
最近更新 更多