谁能告诉我如何在每个时间段内重复调用网络服务。(例如,我想每 5 分钟调用一次网络服务)。在我的应用程序中,我有一个微调器,允许用户选择在多少分钟后必须刷新网络服务。这是我使用倒数计时器编写的代码。
这里我写了逻辑,使得选择旋转器中的“donot刷新”时,应停止计时器。一旦我选择了除第一项以外的任何项目,然后如果我选择了第一项(即不刷新),则计时器不会停止。这里我在 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();
}
}