要调用的主要THEAD的从子线程的方法线程、方法、从子、THEAD

2023-09-07 11:07:41 作者:柠檬草的味道

在我的应用程序,我使用肥皂Web服务调用的基础上,Web服务调用的回复我要显示一些消息。

In my app i am using soap webservice call , based on the webservice call reply i have to display some messages .,

但答复的内容我无法从衍生的子线程执行完此

那么,如何要回主线程和我得到的答复后,显示此

So how to get back to the main thread and display this after the reply i got

希望这是明确的..帮助我如何实现我的要求

Hope this is clear.. help me how to achieve my requirement

{
Thread t1 = new Thread() {
public void run() {

    String threadName = Thread.currentThread().getName();
    // There will be delay in this statement while fetching a data from webservice
    String  returnfromWebservice =   webservice(xmlDetails, "generateid");
    Log.v("returnfromWebservice",returnfromWebservice);
    if(  ! returnfromWebservice.equalsIgnoreCase("nil")){ 
        gotid = returnfromWebservice;
        gotReply=true;
    //  dothis();// I could able to do this because this method contains widgets
    //  I am gettin the error : Only the original thread that created a view hierarchy can touch its views.
    //I understand this is because childthread has no controls on widget 
    /**Suggest me how to get back to main thread*/
    }
    }};
t1.start();
dothis();// so i am doin here after the completion of it 


}

public void dothis{
if(gotReply){
    idtext.setText(gotid);
    genId.setEnabled(false);
    Toast.makeText(WelcomeScorer.this, "Generated ", 500).show();
}
else{
    Toast.makeText(WelcomeScorer.this, "Try Once More ", 500).show();
    idtext.setText(gotid);
}
}

我是新到Android,是否有Android的API来处理这种情况?任何最好的办法

I am new to android, Is there any best approach in android api to handle this situation ??

推荐答案

您应使用以下code从另一个线程触摸你的UI元素

You should use the following code to touch your ui elements from another thread

youractivityname.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

            }
        });

如果你的线程是在同一个活动,您可以使用此功能。否则,你应该用你的活动类对象来运行上面的方法从code您应该调用dothis()。跟帖已完成其工作。从你的,它会调用后立即线程开始它并不会在意线程是否已完成其工作或不dothis方法。

If your thread is in same activity you can use this. Otherwise you should use your activity class object to run the above method.From your code you should call dothis(); after thread has done its job. From your it will call the dothis method immediately after thread has started it wont care whether thread has done its job or not.