下面的代码每 30 秒生成 7 个随机数字。
public class MainActivity extends Activity {
Timer timeoutTimer;
final Random myRandom = new Random();
GenerateTask genTask = new GenerateTask();
static String RAN_TEXT = "text";
class GenerateTask extends TimerTask {
boolean started = false;
public void run() {
if (started) {
System.out.println("generating");
final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);
RAN_TEXT = "";
for(int k=0;k<7;k++){
RAN_TEXT += myRandom.nextInt(10) + " ";
}
runOnUiThread(new Runnable() {
@Override
public void run() {
textGenerateNumber.setText(RAN_TEXT);
}
});
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonGenerate = (Button)findViewById(R.id.generateme);
buttonGenerate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
System.out.println("click");
if (!genTask.started) {
genTask.started=true;
timeoutTimer = new Timer();
timeoutTimer.scheduleAtFixedRate(genTask, 0, 30000);
} else {
genTask.started=false;
timeoutTimer.cancel();
}
}
});
}
}
我将申请时间同步到服务器:
http://server10.instaforex.com:2012/TimeService/TimeService.svc?wsdl
如何将服务器时间同步到我的应用程序?