我生成一个无限时间的随机字符串,并将其设置为EditText。当我不使用runOnUi的时候,app可以在性能更高的新设备上运行。但当我启动线程并给出错误(从错误线程异常调用)时,它在较旧的模型上崩溃。
然后我使用了runOnUi,但它会使速度变得非常慢,并强制关闭它。
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
tryPass.setText(getAlphaNumericString());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
});
thread.start();发布于 2020-02-21 16:55:55
您试图通过在UI线程上调用Thread.sleep(2000);来阻止UI线程。
尝试这样做:
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tryPass.setText(getAlphaNumericString());
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();https://stackoverflow.com/questions/60335091
复制相似问题