gpt4 book ai didi

java - 在android中等待并通知

转载 作者:行者123 更新时间:2023-11-30 03:54:01 27 4
gpt4 key购买 nike

我在我的应用程序中处理一个线程,我遇到了线程等待函数的问题。我使用 runOnUIThread() 创建并显示了我的 UI,我的主 thraed 正在等待我的 UI 线程完成,thraed 完成后我在主线程中做了一些处理,这里的问题是主进程是始终处于等待状态,即使我的线程完成它;我正在为此使用等待和 notify() 函数

      public String LSVerification() {

String t = null;

t="Sample string";
synchronized(this)

{
AndroidHTMLActivity.this.runOnUiThread(ShowDialog) ;//here i call my thread

try {
this.wait();//here i waiting for my thread to finish it's job

//here i do some process after my thread finish it's job
//the problem is here main process always in wait state


} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return t;
}

private Runnable ShowDialog = new Runnable() {
public void run() {
String t = null;
synchronized(ShowDialog)

{
Recognition recg=new Recognition(Activity.this);

recg.show();


this.notify();
}

}
};

最佳答案

在后台执行作业后尝试使用 Hanlders 更新 UI...

Link to Handlers请参阅下面的代码..

private void performOperationInBackgroundThread() {
Thread Servicethread = new Thread(
new Runnable() {
public void run() {
try {
PerformThreadOperation();
DataLoaded = true;
} catch (Exception e) {
ExceptionOccured = true;
e.printStackTrace();
System.out.println(e.getMessage());
}
handler.sendMessage(handler.obtainMessage());
}
}
);
Servicethread.start();
}


static class MyHandler extends Handler{
MyActiviy parentActivity;
@Override
public void handleMessage(Message msg){
// Update your UI here

}
}

MyHandler(MyActiviy activity){
parentActivity = activity;
}
}

MyHandler handler = new MyHandler(this);

关于java - 在android中等待并通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13643744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com