任何人都可以告诉我如何在每个时间段内反复拨打网络服务。(例如,我想每隔5分钟拨打一次网络服务)。 在我的应用程序中,我有一个微调器,允许用户在必须刷新webservice多少分钟后进行选择。这是我用倒数计时器编写的代码。
这里我编写了逻辑,当选择了微调器中的“donot refresh”时,应该停止计时器。一旦我选择了除第一项以外的任何项目,然后如果我选择第一项(即,不要刷新),则计时器不会停止。我在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();
}
}
答案 0 :(得分:2)
尝试以下代码
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();
}
};
}
如果它是正确的那么就做对了。
答案 1 :(得分:0)