How to convey the meaning of progress from flow to activity?

I'm having a design problem sending the value of the execution line from the class called from the Thread in Activity class to update the GUI, as shown below.

[The code snippet is not compiled for explanation 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 guidance would be greatly appreciated.

+6
java android multithreading progressdialog handlers
source share
3 answers

You can create the updateProgressBar method in class A, and then pass a reference to class A to class B. Then class B can call the callback function in (maybe pass an int or something to indicate how far the progress has progressed). Updating the user interface from another thread, then the user interface thread may cause problems. Fortunately, the Activity class has a method of "runOnUiThread (Runnable action)". Therefore, to establish progress, you can do something like:

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

I used the Handler to achieve this effect. Create it in the Activity in which you create the ProgressDialog , and then pass the Handler to the maethod from which you want to get the progress. You can then send the message back to the calling Activity to update the move:

 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); } } 
+3
source share

I always use this type of pattre and gr8 works for me ...

 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); } } } 
+3
source share

All Articles