6

我有一个设计问题,从 Activity 类中的线程调用的类发送进度条值以更新 GUI,如下所示

[代码片段不编译,仅用于解释]:

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
       }

    }

}

任何帮助或指导将不胜感激

4

3 回答 3

3

我用 aHandler来达到这个效果。Activity在你创建的那个中创建它ProgressDialog,然后将它传递给Handler你想要从中获得进度的方法。然后您可以向调用方发送一条消息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);
    }
}
于 2011-02-08T09:18:07.583 回答
3

我总是使用这种模式并为我工作 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);
        }

    }
}
于 2012-03-29T10:13:34.203 回答
2

您可以在 A 类中创建一个 updateProgressBar 方法,然后向 B 类传递对 A 类的引用。然后 B 类可以在 A 中调用回调函数(可能传递一个 int 或其他东西来指示进度有多远)。从不同的线程更新 UI,然后 UI 线程往往会导致问题。幸运的是,Activity 类有方法“runOnUiThread(Runnable action)”。因此,要设置进度,您可以执行以下操作:

while(response.read()){
  //do stuff here
int progress = getProgress(); //set the progress to something
  a.runOnUiThread(new Runnable(){
    a.updateProgress(progress);
  });
}
于 2011-02-08T08:51:27.113 回答