我正在无限时间生成一个随机字符串并将其设置为 EditText。 当我没有使用 runOnUi 应用程序时,它正在具有高性能的较新设备上工作。但当我启动线程并给出错误(从错误的线程异常调用)时,它在旧模型上崩溃
然后我使用了 runOnUi,但它使速度 super 慢并强制关闭它。
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();
最佳答案
您试图通过在 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();
关于java - runOnUi 减慢应用程序速度并使其强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60335091/