Reputation: 192
I want to create a WebWiew app that's will download multiple files - by opening multiple windows - each window with another file url. (using window.open() in js)
My code:
package aviad.com.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://matanya.hagitbagno.co.il/dafYomi/index.html";
WebView webView = (WebView) findViewById(R.id.webview);
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
}
}
How can I do that?
Upvotes: 1
Views: 766
Reputation: 1010
when ever you click download button/url it will call shouldOverrideUrlLoading and onDownloadStart
two method will called, shouldOverrideUrlLoading
will not do anything in this case but,but onDownloadStart
can help us to fix your issue.
keep setDownloadListener
to your webview like following.
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
Log.d(TAG, "onDownloadStart: ");
if (Util.isOurServer(url)) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
request.setDescription("Downloading file...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading...", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType(mimetype);
intent.setData(Uri.parse(url));
startActivity(intent);
}
//registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
Upvotes: 1