[英]I can't update more than one on my infinite thread in button click
At the below code i try to take the user's current location each second as x and y axises.At the first time of loop,the code works.But second and so on it doesnt work.What is the missing thing on this code.What should i do.Thanks..在下面的代码中,我尝试每秒将用户的当前位置作为 x 和 y 轴。在循环的第一次,代码有效。但是第二次等等它不起作用。这段代码中缺少什么。什么我应该做吗。谢谢。
new Thread(new Runnable(){
@Override
public void run() {
while (true) {
runOnUiThread(new Runnable() {
public void run() {
textViewXAltitude.setText(String.valueOf(gps.getLatitude()));
textViewYAltitude.setText(String.valueOf(gps.getLongitude()));
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
you should do this by Handler;for example:您应该由 Handler 执行此操作;例如:
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
textViewX.setText(String.valueOf(i++));
textViewY.setText(String.valueOf(j++));
}
}
};
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
}
}, 0, 1000);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.