Downloader{ private static volatile Downloader iDownloader=null; private Downloader(){ } public static Downloader createDownloader(){ if(iDownloader==null){ synchronized(Downloader.class){ if(iDownloader==null) iDownloader=new Downloader(); } } return iDownloader; }
new Thread(new Runnable() { @Override public void run() { File out; Downloader DDL; DDL=new Downloader(); out=new File(Environment.getExternalStorageDirectory() + "/DestFileName.txt"); DDL.DownloadFile("SourceURL",out); } }).start();
Downloader dlA = new Downloader(...); Downloader dlB = new Downloader(...); ProgressListener listener = new ProgressListener() { @Override public void onProgress(int n) { dlA.removeListener(this); dlB.removeListener(this); } } dlA.addListener(listener); dlB.addListener(listener);
public class MyActivity extends Activity implements OnDownloadCompleteListener { private void startDownload() { //inside whatever method Downloader downloader = new Downloader(this); //'this' implements OnDownloadCompleteListener downloader.go(); } @Override public void onDownloadFinished(boolean success) { //implementation } }
public void actionPerformed(ActionEvent e) { //run(); Downloader t = new Downloader(); t.start(); }
class MyTask extends AsyncTask...{ onPreExecute(){ showProgress(); } doInbackground(){ //this by itself is a thread so you may need to change the Downloader code //it should be normal class, not thread. Downloader loader = new Downloader(); loader.doSomeWork(); //loader.start(); } onPostExecute(){ //you should NOT hide the progress at the end of Downloader() hideProgress(); }
Downloader d = new Downloader(...); MyProgressListener l = new MyProgressListener(); l.downloader = d; d.addListener(l); d.run();
List<Channel> channels = ... ; Downloader downloader = new Downloader(channels); downloader.setOnSucceeded(e -> { List<Article> articles = downloader.getValue(); // update UI with articles... }); Thread t = new Thread(downloader); t.setDaemon(true) ; // will not prevent application exit... t.start();
String html = ""; try { Downloader d = new Downloader(); d.execute(editText.getText().toString()); html = d.get(); Log.d("HTML CAME BACK", html); news = rfm.getFeed(html);
// create the object which downloads the HTML Downloader downloader = new Downloader(); // Ask it to download, and store the result into a String variable String downloadedHtml = downloader.download(); // create the object which can search into a String for .swf references Searcher searcher = new Searcher(); // pass it the String to search into searcher.searchSwfIn(downloadedHtml);
InputStreamAndLength downloadStreamAndLength = downloadInputStream(downloadConfig, distribution); progress.info(progressLabel, "downloaded with " + downloadSpeed(downloadStartedAt,length)); } finally { downloadStream.close();
new Timer(500, new ActionListener(){ public void actionPerformed(ActionEvent e) { //run(); Downloader t = new Downloader(); t.start(); } }).start();
InputStreamAndLength downloadStreamAndLength = downloadInputStream(downloadConfig, distribution); progress.info(progressLabel, "downloaded with " + downloadSpeed(downloadStartedAt,length)); } finally { downloadStream.close();
PhoneGap.addConstructor(function() { PhoneGap.addPlugin("downloader", new Downloader()); PluginManager.addService("Downloader", "com.phonegap.plugins.downloader.Downloader"); });
function Downloader() {} Downloader.prototype.downloadFile = function(fileUrl, params, win, fail) { //Make params hash optional. if (!fail) win = params; PhoneGap.exec(win, fail, "Downloader", "downloadFile", [fileUrl, params]); }; PhoneGap.addConstructor(function() { PhoneGap.addPlugin("downloader", new Downloader()); PluginManager.addService("Downloader", "com.phonegap.plugins.downloader.Downloader"); }); if(!window.plugins) { window.plugins = {}; } if (!window.plugins.downloader) { window.plugins.downloader = new Downloader(); }
'use strict'; const util = require('util'); const EventEmitter = require('events'); function Downloader() { var self = this; self.totalEmitted = 0; this.on('downloaded', function() { self.totalEmitted ++; console.log(self.totalEmitted); }); } util.inherits(Downloader, EventEmitter); Downloader.prototype.download = function(delaySecs) { setTimeout(() => { this.emit('downloaded', delaySecs); }, delaySecs*1000); }; var downloader = new Downloader(); downloader.download(1); module.exports = Downloader;
public static void gogogo() { t= new Downloader(); t.start(); } public void run() { while(true){ doStuff(); //the code that i want repeatedly called Thread.sleep(500); } }
public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(5); for (String file : args) { executorService.submit(new Downloader(file)); } executorService.awaitTermination(100, TimeUnit.SECONDS); }
ExecutorService service = Executors.newCachedThreadPool(); Downloader down = new Downloader("http:xxxxx", new File("./references/word.txt"), new File("./references/words.txt")); Downloader down2 = new Downloader("http:xxxx", new File("./references/word1.txt"), new File("./references/words1.txt")); service.invokeAll(Arrays.asList(down, down2));
ArrayList<String> urls = new ArrayList<String>(10); // ... ArrayList<Thread> threads = new ArrayList<Thread>(10); for (String url : urls) { Thread t = new Downloader(url); threads.add(t); t.start(); } for (Thread t : threads) { try { // do not wait for other threads in main UI thread! t.join(); //((Downloader) t).getMimeType(); //((Downloader) t).getFileSize(); } catch (InterruptedException e) { e.printStackTrace(); } }