如何检测和关闭JavaScript对话框关闭按钮以取消弹出的android

时间:2018-05-12 02:43:48

标签: javascript android android-webview webchromeclient android-chrome

我正在使用webview来显示网址。它会显示带有关闭按钮的滚动地图的弹出窗口。

https://www.onemap.sg/main/v2/ [仅在手机中显示]

当我点击关闭按钮时,它无法正常工作。对话框没有关闭。

此外,在背面,地图未显示在webview中。你可以看到截图

enter image description here

我的代码如下:

我无法猜出应该添加什么来关闭对话框。请帮我解决。

public class WebViewActivity extends AppCompatActivity {
    private ImageView backIcon;
    private ImageView homeIcon;

    private DownloadManager dm;
    private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            if (MainActivity.this.downloadReference == intent.getLongExtra("extra_download_id", -1)) {
                Query q = new Query();
                q.setFilterById(new long[]{intent.getLongExtra("extra_download_id", -1)});
                Cursor c = MainActivity.this.dm.query(q);
                if (c.moveToFirst() && c.getInt(c.getColumnIndex("status")) == 8) {
                    String title = c.getString(c.getColumnIndex("title"));
                    File file = new File(Environment.getExternalStorageDirectory() + "/Download/" + title);
                    if (!file.isDirectory()) {
                        file.mkdir();
                    }



                    Intent testIntent = new Intent("android.intent.action.VIEW");
                    testIntent.setType("application/pdf");
                    testIntent.setDataAndType(Uri.fromFile(file), "application/pdf");

                    try {
                        MainActivity.this.startActivity(testIntent);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(MainActivity.this, "No application available to view PDF", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    };
    private long downloadReference;
    private String location;
    private WebView mWebview;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView((int) R.layout.activity_main);
//        Toolbar mToolbar = (Toolbar) findViewById(R.id.tool_bar);

//        setSupportActionBar(mToolbar);
        this.mWebview = (WebView) findViewById(R.id.webview);
        this.mWebview.getSettings().setJavaScriptEnabled(true);
        this.mWebview.getSettings().setLoadWithOverviewMode(true);
        this.mWebview.getSettings().setUseWideViewPort(true);
        this.mWebview.getSettings().setBuiltInZoomControls(true);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            if(0 != (getApplicationInfo().flags = ApplicationInfo.FLAG_DEBUGGABLE)){
                WebView.setWebContentsDebuggingEnabled(true);
            }
        }
    loadWebView();

}

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (mWebview.canGoBack()) {
                        mWebview.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }

        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (mWebview.canGoBack()) {
            mWebview.goBack();
        } else {
            finish();
        }
    }


    private void loadWebView() {

        this.mWebview.setWebChromeClient(new WebChromeClient(){

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Log.e("urlss",url);
                return super.onJsAlert(view, url, message, result);
            }
        });

        this.mWebview.setWebViewClient(new WebViewClient() {
            final ProgressDialog pd = ProgressDialog.show(MainActivity.this, "", "Loading...Please wait!", true);

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            }
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed(); // Ignore SSL certificate errors
            }

            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                this.pd.show();
            }

            public void onPageFinished(WebView view, String url) {
                Log.e("url",url);
                this.pd.dismiss();
                    MainActivity.this.mWebview.loadUrl("https://www.sla.gov.sg/INLIS/");
                }

//code for feedback//
                       @Override
           public boolean shouldOverrideUrlLoading(WebView view, String url) {
               if( url.startsWith("http:") || url.startsWith("https:") ) {
                   return false;
               }

               // Otherwise allow the OS to handle it
               else if (url.startsWith("tel:")) {
                   Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                   startActivity(tel);
                   return true;
               }
               Log.d("Main URL ", url);

               return false;
           }


        });
        this.mWebview.loadUrl("https://www.sla.gov.sg/inlis/#/");
        this.mWebview.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                Request request = new Request(Uri.parse(url));
                request.setMimeType("pdf");
                request.addRequestHeader("cookie", CookieManager.getInstance().getCookie(url));
                request.addRequestHeader("User-Agent", userAgent);
                request.setDescription("Downloading file...");
                request.setTitle(URLUtil.guessFileName(url, contentDisposition, "pdf"));
                request.allowScanningByMediaScanner();
                if (VERSION.SDK_INT >= 11) {
                    request.setNotificationVisibility(0);
                } else {
                    request.setShowRunningNotification(true);
                }
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, "pdf"));
                MainActivity.this.dm = (DownloadManager) MainActivity.this.getSystemService(DOWNLOAD_SERVICE);
                MainActivity.this.downloadReference = MainActivity.this.dm.enqueue(request);
                MainActivity.this.registerReceiver(MainActivity.this.downloadReceiver, new IntentFilter("android.intent.action.DOWNLOAD_COMPLETE"));
            }
        });
    }


}

0 个答案:

没有答案