android - 在2秒内从后台动态调用网址

时间:2017-06-09 10:01:28

标签: java android

我在Android应用程序中有一个url,它从异步任务中获取数据,如下所示

 new AsyncParam().execute("http://10.0.2.2:88/API/web/app_dev.php/get/1/7");

请问如何确保每隔一秒动态调用上述网址

1 个答案:

答案 0 :(得分:0)

Use this:
//Declare globally

Handler handler = new Handler();
    Runnable runnable;

//then whereever you want to call your url
runnable = new Runnable() {
            @Override
            public void run() {
                new AsyncParam().execute("http://10.0.2.2:88/API/web/app_dev.php/get/1/7");
//this will call your url after every one sec
                handler.postDelayed(runnable,1000);
            }
        };
//in place of 1000 provide initial delay you can give 0 also if you want to call immediately
        handler.postDelayed(runnable,1000);