Answer the question
In order to leave comments, you need to log in
Java/Android - 3rd party class interaction - newbie question?
Practicing Java programming for Android.
There was a question that I can not correctly solve and I want to hear your advice.
The example is simplified, but the essence is the following - I am writing an Android program that receives a list of links from the server and downloads them to the phone, here are two classes that are used for downloading:
<font color="black"><font color="#0000ff">public</font> <font color="#0000ff">class</font> Download {<br/> <br/> <font color="#0000ff">public</font> <font color="#0000ff">long</font> getTotal() {<br/> <font color="#0000ff">return</font> total;<br/> }<br/> <br/> <font color="#0000ff">protected</font> <font color="#0000ff">void</font> setTotal(<font color="#0000ff">long</font> total) {<br/> <font color="#0000ff">this</font>.total = total;<br/> }<br/> <br/> <font color="#0000ff">private</font> <font color="#0000ff">long</font> total = 0;<br/> <br/> <font color="#0000ff">public</font> boolean download(String link, String path) throws IOException {<br/> <br/> <font color="#008000">// download the file</font><br/> InputStream input = <font color="#0000ff">new</font> BufferedInputStream(url.openStream());<br/> OutputStream output = <font color="#0000ff">new</font> FileOutputStream(outputFile);<br/> <br/> <font color="#0000ff">int</font> count = 0;<br/> <font color="#0000ff">byte</font> data[] = <font color="#0000ff">new</font> <font color="#0000ff">byte</font>[1024];<br/> <br/> <font color="#0000ff">while</font> ((count = input.read(data)) != -1) {<br/> <font color="#0000ff">this</font>.setTotal(<font color="#0000ff">this</font>.getTotal() + count);<br/> output.write(data, 0, count);<br/> }<br/> <br/> <font color="#0000ff">return</font> <font color="#0000ff">true</font>;<br/> }<br/> }<br/> <br/> <font color="#0000ff">public</font> <font color="#0000ff">class</font> DownloadBatch {<br/> <br/> <font color="#0000ff">public</font> boolean download(ArrayList<String[]> tasks) throws IOException {<br/> <br/> Download loader = <font color="#0000ff">new</font> Download();<br/> <br/> <font color="#0000ff">for</font> (String[] task : tasks) {<br/> loader.download(task[0], task[1]);<br/> }<br/> <br/> <font color="#0000ff">return</font> <font color="#0000ff">true</font>;<br/> }<br/> }</font><br/> <br/> <font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>
Answer the question
In order to leave comments, you need to log in
If the Download and DownloadBatch classes cannot be changed, then there is no way, because the progress state knows only loader
declared in DownloadBatch.download()
and does not exist outside this method.
If, however, the code can be changed, you can adapt the example from this article: " Processes and threads in Android: writing AsyncTask correctly " (just read the comments).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question