Android - DownloadListener or AsyncTask
am trying to download from web page some files by clinking the url with webview handling the download not the browser
if i use DownloadListener it works perfectly with one problem i cant see the progressbar
if i use the AsyncTask i have to put the url in the code to download it i can just click the url and start downloading
my question is how can i let the AsyncTask download any url from the web without sitting the
downloadFile.execute("the url to the file you want to download");
or how i can create progressbar for DownloadListener
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
myProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
new Thread(myThread).start();
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(50);
webview.getSettings().setUseWideViewPort(true);
webview.setVerticalScrollBarEnabled(false);
webview.setHorizontalScrollBarEnabled(false);
webview.loadUrl("http://localhost/index.php");
webview.setWebViewClient(new DownloadWebViewClient());
webview.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
InputStream is;
try {
URL u = new URL(url);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);
con.connect();
is = con.getInputStream();
// Path an开发者_StackOverflow社区d File where to download the APK
String path = Environment.getExternalStorageDirectory() + "/apdroid/";
String fileName = url.substring(url.lastIndexOf('/') + 1);
File dir = new File(path);
dir.mkdirs(); // creates the download directory if not exist
File outputFile = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
// Save file from URL to download directory on external storage
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
String name = Environment.getExternalStorageDirectory() + "/apdroid/" + url.substring(url.lastIndexOf('/') + 1);
intent.setDataAndType(Uri.fromFile(new File(name)), "application/vnd.android.package-archive");
startActivity(intent);
}catch (IOException e) {
e.printStackTrace();
}
}
});
}
protected void install(String fileName) {
// TODO Auto-generated method stub
}
private Runnable myThread = new Runnable() {
@Override
public void run() {
while (myProgress < 100) {
try {
myHandle.sendMessage(myHandle.obtainMessage());
Thread.sleep(1000);
} catch (Throwable t) {
}
}
}
Handler myHandle = new Handler() {
@Override
public void handleMessage(Message msg) {
myProgress++;
myProgressBar.setProgress(myProgress);
}
};
};
private class HelloWebViewClient extends WebViewClient {
@Override
public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) {
try {view.stopLoading();} catch(Exception e){}
try {view.clearView();} catch(Exception e){}
view.loadUrl("file:///android_asset/wifi.html");
}
}
i just want to have ProgressBar when i download any file from my page
and i cant use asyncTask because i have to put the files in the code not by clicking at them
You should probably be overriding the URL loading
process, and recognize by some way if any URL is being loaded whose resource you would want to download.
As soon as you detect this, stop loading the page and start the AsyncTask with this URL.
精彩评论