根据按钮点击,我必须做一些需要一些时间的处理。所以我决定在主UI线程的单独线程中执行此操作。
现在,基于单独线程中的计算,我在UI线程的主类中调用了一个函数,从该线程创建了这个新线程。在此功能中,我更新了UI。我被告知这不会起作用,因为我需要调用主UI线程。
有人可以帮帮我吗?
@Override
public void onListItemClicked(int index, Map<String, Object> data) {
new Thread(new Runnable() {
@Override
public void run() {
// Issue command() on a separate thread
wasCommandSuccess(command());
}
}).start();
}
private void wasCommandSuccess(boolean result){
if (result == false){
getUI(BasicUI.class).showAlert("Command failed!", "Unable to access");
}
}
答案 0 :(得分:1)
你应该在runOnUiThread()中调用函数wasCommandSuccess;所以你应该有这样的代码:
@Override
public void onListItemClicked(int index, Map<String, Object> data) {
new Thread(new Runnable() {
@Override
public void run() {
// Issue command() on a separate thread
final boolean result = command();
// you need to pass your context (any of Activity/Service/Application) here before this
context.runOnUiThread(new Runnable() {
@Override
public void run() {
wasCommandSuccess(result);
}
});
}
}).start();
}
private void wasCommandSuccess(boolean result){
if (result == false){
getUI(BasicUI.class).showAlert("Command failed!", "Unable to access");
}
}