[英]runOnUi slows down the app and make it forced close
i am generating a random string for infinite time and setting it to a EditText.我正在无限时间内生成一个随机字符串并将其设置为 EditText。 when i was not using runOnUi app was working on newer devices which have high capability.当我不使用 runOnUi 应用程序时,它正在处理具有高性能的较新设备。 but it crashes on older model when i start the thread and gave error(called from wrong thread exception)但是当我启动线程并给出错误(从错误的线程异常调用)时,它会在旧模型上崩溃
Then i used runOnUi but it makes the super slow and force close it.然后我使用了 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();
You're trying to block UI thread by calling Thread.sleep(2000);
您试图通过调用Thread.sleep(2000);
来阻止 UI 线程Thread.sleep(2000);
on UI thread.在 UI 线程上。
Try this way:试试这个方法:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.