Timer.schedule即使在句点

时间:2017-08-06 08:38:28

标签: java android timertask

我是一名学生正试图为我的一个模块为我的miniproject创建一个应用程序,我正在尝试创建一个应用程序,每隔几秒就从服务器抓取一次数据,这样就可以更新。我尝试使用java计时器和timerTask重复运行代码,但程序只运行一次,并且在实现计时器后,get-button无法正常工作(假设立即获取数据)。 Android Emulator

    public class MainActivity extends AppCompatActivity implements OnClickListener{

private Button speed;
private TextView result;
Timer timer;
TimerTask timerTask;
private TextView sSpeed;
final StringBuilder builder = new StringBuilder();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    result = (TextView) findViewById(R.id.result);
    sSpeed = (TextView) findViewById(R.id.sSpeed);
    speed = (Button) findViewById(R.id.get_button);
    speed.setOnClickListener(new View.OnClickListener(){
       @Override
        public void onClick(View view) {
           getWebsite();
       }
    });
    View aboutButton = this.findViewById(R.id.about_button);
    aboutButton.setOnClickListener(this);
    View exitButton = this.findViewById(R.id.exit_button);
    exitButton.setOnClickListener(this);
}

 public void onClick(View v) {
    switch (v.getId()){
        case R.id.get_button:
            getWebsite();
            break;
        case R.id.about_button:
            Intent i = new Intent(this, About.class);
            startActivity(i);
            break;
        case R.id.exit_button:
            finish();
            break;
    }
}

private void getWebsite(){
    new Thread(new Runnable() {
        @Override
        public void run() {


            try{
                Document doc = Jsoup.connect("http://10.0.2.2:8080/Start_Stop_buttons_UTF8.html").get();
               // Elements element = doc.getElementsByTag("p");
                Elements element = doc.select("p");
                //String title = doc.title();
                builder.append(title).append("\n");
                for (Element tag : element){
                    builder.append("\n\n").append(tag.text());
                }

            }catch(IOException e){
                //e.printStackTrace();
                builder.append("Error : ").append(e.getMessage()).append("\n");
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String a = builder.toString(); // parse data from html into new string
                    a = a.substring(a.indexOf(":")+1, a.indexOf("Control")).trim();//trim string content
                    String b = builder.toString();
                    b = b.substring(11,b.indexOf(":")+1).trim();

                    double speed = Double.parseDouble(a);//convert string into double
                    if (speed<1000)
                        Log.i("HTML text","too slow");
                    else if((speed> 1500))Log.i("HTML text","too fast!");

                    result.setText(a);
                    sSpeed.setText(b);
                }
            });
        }

    }).start();

}

protected void onResume() {
    super.onResume();
    startTimer();
}

public void startTimer(){
    timer = new Timer();
    timerTask = new TimerTask() {
        @Override
        public void run() {
            getWebsite();

        }
    };
    timer.schedule(timerTask,1500,3000);
}

public void stopTimer(){
    if(timer != null){
        timer.cancel();
        timer.purge();
    }
}

}

我是否正确实现了计时器以反复运行getwebsite()并且能够在点击get-button时获得即时更新?或者有更好的方法使用不同的方法实现这些功能吗?

1 个答案:

答案 0 :(得分:0)

您永远不会在startTimer中调用ClickListener方法。您拨打getWebsite一个电话。将您的通话更改为startTimer

 speed.setOnClickListener(new View.OnClickListener(){
       @Override
        public void onClick(View view) {
           startTimer();
       }
    });

您也可能想要在开始新计时之前检查计时器是否已经运行。为此,请在null方法上指定stopTimer值,例如

public void stopTimer(){
    if(timer != null){
        timer.cancel();
        timer.purge();
        timer = null;
    }
}

您的startTimer看起来像这样

public void startTimer(){
    if(timer != null) return; // don't start multiple timers
    timer = new Timer();
    timerTask = new TimerTask() {
        @Override
        public void run() {
            getWebsite();

        }
    };
    timer.schedule(timerTask,1500,3000);
}