简体   繁体   English

如何将进度值从线程传递给活动?

[英]How to pass progress value from thread to activity?

I am having a design issue sending progress bar value from class called from a Thread in Activity class to update the GUI, as the following 我有一个设计问题,从活动类中的Thread调用的类中发送进度条值以更新GUI,如下所示

[The code snippet don't compile it's for explaining only]: [代码片段不编译它仅用于解释]:

Class A : Extend Activity {
  new Thread(new Runnable() 
    {
       public void run() 
       {
            B objB = new B();
            objB.DownloadFile();
        }
    }).start();
}

Class B {
    public void DownloadFile()
    {
       ... some work [preparing SOAP request]
       while(response.read())
       {
         //send calculated progress to Class A to update the progress value
       }

    }

}

Any help or guide would be greatly appreciated 任何帮助或指南将不胜感激

I've used a Handler to achieve this effect. 我用过Handler来达到这个效果。 Create it in the Activity that you create the ProgressDialog in, then pass the Handler into the maethod you want to get the progress from. 在您创建ProgressDialogActivity中创建它,然后将Handler传递给您想要从中获取进度的maethod。 Then you can send a message back to the calling Activity to update the progress: 然后,您可以将消息发送回调用的Activity以更新进度:

public class ClassA extends Activity {
    ...
    private static final int HANDLER_MESSAGE_PERFORM_DIALOG_UPDATE = 0;
    ...
    new Thread(new Runnable() 
    {
        public void run() 
        {
            B objB = new objB();
            objB.DownloadFile(handler);
        }
    }).start();
    ...
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
            case Constants.HANDLER_MESSAGE_PERFORM_DIALOG_UPDATE:
                progress.setProgress(msg.arg1);
                break;
            default:
                Log.w("TAG_NAME","handleMessage / Message type not recognised / msg.what = "+String.valueOf(msg.what));
            }
        }
    };
}

Class B 
{
    public void DownloadFile(Handler handler)
    {
       ... some work [preparing SOAP request]
       while(response.read())
       {
         //send calculated progress to Class A to update the progress value
           sendMessage(handler,HANDLER_MESSAGE_PERFORM_DIALOG_UPDATE,progress);
       }
    }
    private void sendMessage(Handler handler,int what, int arg1){
        Message msg = Message.obtain();
        msg.what = what;
        msg.arg1 = arg1;
        handler.sendMessage(msg);
    }
}

I always use this kind of pattre and works gr8 for me... 我总是使用这种pattre并为我工作gr8 ...

class A{
    new Thread(new Runnable() 
    {   
            public void run() 
            {
            B objB = new B(new ListnerClass());
            objB.DownloadFile();
            }
    }).start();

    class ListnerClass implements B.ValueListner{
        update(int v){
            doWork();
        }
    }


}

class B{

    ValueListner listner;

    interface ValuListner{
        update(int i);
    }
    B(ValueListner listner){
        this.listner = listner;
    }
    public void DownloadFile()
        {
            ... some work [preparing SOAP request]
        while(response.read())
        {
            listner.update(value);
        }

    }
}

You could make an updateProgressBar method in class A and then pass class B a reference to class A. Class B could then call the callback function in A (probably pass an int or something to indicate how far the progress is). 您可以在类A中创建updateProgressBar方法,然后向类B传递对类A的引用。然后,类B可以调用A中的回调函数(可能传递一个int或者其他东西以指示进度有多远)。 Updating the UI from a different thread then the UI thread tends to cause problems. 从不同的线程更新UI然后UI线程往往会导致问题。 Luckily the Activity class has the method "runOnUiThread(Runnable action)". 幸运的是,Activity类的方法是“runOnUiThread(Runnable action)”。 So to set the progress you could do something like: 因此,要设置进度,您可以执行以下操作:

while(response.read()){
  //do stuff here
int progress = getProgress(); //set the progress to something
  a.runOnUiThread(new Runnable(){
    a.updateProgress(progress);
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM