WebWiew - 打开多个窗口

时间:2018-05-03 14:57:34

标签: java android webview

我想创建一个WebWiew应用程序,它将通过打开多个窗口来下载多个文件 - 每个窗口都有另一个文件URL。 (在js中使用window.open())

我的代码:

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);

    }
}

我该怎么做?

1 个答案:

答案 0 :(得分:0)

每当您点击下载按钮/网址时,它都会调用 shouldOverrideUrlLoading and onDownloadStart 两种方法,shouldOverrideUrlLoading 在这种情况下不会做任何事情,但是 onDownloadStart 可以帮助我们解决您的问题问题。 将 setDownloadListener 保持在您的网络视图中,如下所示。

 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));


        }