java Android计时器任务,当我停止运行并关机时
我使用安卓java,
我有两个按钮,按钮A是运行计时器,它可以在5秒钟内改变许多词汇,
按钮B是停止计时器,当我按下按钮A时,计时器运行正常,按下按钮B也正常
但当我按下按钮A->;按钮B->;按钮A,它将关闭,我如何解决这个问题
谢谢
这是我的代码:
private Button.OnClickListener handbtn=new Button.OnClickListener(){//ButtonB
@Override
public void onClick(View view) {
if(timer != null) {
timer.cancel();
timer.purge();
timer = null;
}
url ="https://kei-sei.com/cram/n5.json";
level.setText("N5");
String result = dbn5.executeQuery(url);
try{
JSONArray array = new JSONArray(result);
Random random=new Random();
int tmp=random.nextInt(array.length()-1);
JSONObject jsonObject = array.getJSONObject(tmp);
String word = jsonObject.getString("word");
jp.setText(word);
}
catch(JSONException e) {
}
}
};
public void begin() {
timer.schedule(new MyTimerTask(), SEC*1000, SEC*1000) ;
}
public class MyTimerTask extends TimerTask
{
public void run()
{
url ="https://kei-sei.com/cram/n5.json";
level.setText("N5");
String result = dbn5.executeQuery(url);
try{
JSONArray array = new JSONArray(result);
Random random=new Random();
int tmp=random.nextInt(array.length()-1);
JSONObject jsonObject = array.getJSONObject(tmp);
String word = jsonObject.getString("word");
jp.setText(word);
}
catch(JSONException e) {
}
}
};
private Button.OnClickListener autobtn=new Button.OnClickListener(){//ButtonA
@Override
public void onClick(View view) {
begin();
}
}
};
共 (0) 个答案