0

I am using webview to display the url. It displays the popup with scrolling map with close button.

https://www.onemap.sg/main/v2/ [this displays only in mobile]

When I click close button, it's not working. Dialog is not closing.

Moreover at the backside, map is not displaying in webview. You can see the screenshot

enter image description here

My code is as follows:

I couldn't guess what should be added to close the dialog. Kindly help me to resolve.

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


}
Shadow
  • 6,864
  • 6
  • 44
  • 93
  • just a side note - use of "m" prefix is ensure you when can stop using `this.` and setting local vars instead. You use both which is odd. – Marcin Orlowski May 12 '18 at 02:51
  • Thanks. Can you please elaborate. I couldn't get you where I make mistake. :( Let me close this by accepting after corrected. – Shadow May 12 '18 at 02:54

0 Answers0