gpt4 book ai didi

java - 一段时间后重复调用webservice

转载 作者:行者123 更新时间:2023-12-02 07:47:56 27 4
gpt4 key购买 nike

谁能告诉我如何在每个时间段重复调用一个Web服务。(例如我想每5分钟调用一次Web服务)。在我的应用程序中,我有一个微调器,允许用户选择在多少分钟后必须刷新网络服务。这是我使用倒计时器编写的代码。

在这里,我编写了逻辑,以便当在微调器中选择“不刷新”时,计时器应该停止。一旦我选择了第一个项目以外的任何项目,然后如果我选择了第一个项目(即不刷新),计时器就不会停止。这里我在 onfinish() 方法中调用 ws 来重复调用。

 private String[] refreshtimes = { "do not refresh","1 minute Refresh",  "5minute           Refresh",
"15 minute Refresh", "30 minute Refresh", "45 min Refresh",
"60 minute Refresh" };

sp_refresh = (Spinner) findViewById(R.id.refresh);
ArrayAdapter<String> spdptr = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
refreshtimes);
sp_refresh.setAdapter(spdptr);

sp_refresh.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {

if(position!=0 )
{
int time=0;

switch (position) {
case 1:
time=1;
break;
case 2:
time=5;
break;
case 3:
time=15;
break;
case 4:
time=30;
break;
case 5:
time=45;
break;
case 6:
time=60;
break;
default:
break;
}
counter = new MyCount(time*1000,1000);
counter.start();
}
else if(position==0&&counter!=null)
{
counter.cancel();
counter=null;
Toast.makeText(getApplicationContext(), "u r in elsee",10000).show();
}

}
public class MyCount extends CountDownTimer{

public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onFinish() {
// tv.setText("done!");
callservice();
Toast.makeText(getApplicationContext(), "hi..",10000).show();
//onCreate(savedInstanceState);

this.start();
}

@Override
public void onTick(long millisUntilFinished) {
// tv.setText("”Left: " + millisUntilFinished/1000);
Toast.makeText(getApplicationContext(), "Left: " + millisUntilFinished/1000,10000).show();
}

}

最佳答案

尝试下面的代码

    package com.Test_dia;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class Test_diaActivity extends Activity {

String[] refreshtimes = { "do not refresh", "1 minute Refresh",
"5minute Refresh", "15 minute Refresh",
"30 minute Refresh", "45 min Refresh", "60 minute Refresh" };
private Spinner Spi;
private int time;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Spi = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> spdptr = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
refreshtimes);
Spi.setAdapter(spdptr);

Spi.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
if (position > 0) {

switch (position) {
case 1:
time = 1;
break;
case 2:
time = 5;
break;
case 3:
time = 15;
break;
case 4:
time = 30;
break;
case 5:
time = 45;
break;
case 6:
time = 60;
break;
default:
break;
}

call_timmer(time);

}
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
}

private void call_timmer(int i) {
// TODO Auto-generated method stub

int t = i * 60000;

Timer myt = new Timer();
myt.schedule(new TimerTask() {

@Override
public void run() {
// TODO Auto-generated method stub

timerMethod();
}
}, 0, 60000);
}

private void timerMethod() {
this.runOnUiThread(doSomething);
}

private Runnable doSomething = new Runnable() {
public void run() {
// Your code for doing something
Toast.makeText(Test_diaActivity.this, "dhaval Sodha Parmar",
Toast.LENGTH_LONG).show();
}
};
}

如果是对的,就改正。

关于java - 一段时间后重复调用webservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10581361/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com