Code example for WebView: Displaying web pages

Methods: addJavascriptInterfacegetSettingsloadUrlsetWebChromeClientsetWebViewClient, setDownloadListener

2
        setTitle(getIntent().getStringExtra("NAME"));
        browser = (WebView)findViewById(R.id.browser);
        CookieManager.getInstance().setAcceptCookie(true);
        EduxCookieStore.getInstance(this).updateCookieManager(CookieManager.getInstance());
 
        browser.setWebViewClient(new EduxWebClient());
        browser.setWebChromeClient(new EduxChromeClient());
        browser.addJavascriptInterface(new EduxJSInterface(), "app");
        browser.getSettings().setJavaScriptEnabled(true);
        browser.getSettings().setAllowFileAccess(true);
 
        dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
         
        browser.setDownloadListener(new DownloadListener() {
			 
			public void onDownloadStart(String url, String userAgent,
					String contentDisposition, String mimetype, long contentLength) {
				String[] urlSplit = url.split("/");
				String name = urlSplit[urlSplit.length-1];
				 
		        Request request = new Request(
		                Uri.parse(url));
		        request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie("edux.fit.cvut.cz"))
		        	.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name)
		        	.setTitle(name);
		        dm.enqueue(request);
				 
			} 
		}); 
         
        registerReceiver(onComplete,
                         new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        registerReceiver(onNotificationClick,
                         new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED));
         
        String url = getIntent().getStringExtra("URL");
         
        if (url.endsWith(".1") || url.endsWith(".2")) // Ugly edux quick fix
        	url = url.substring(0, url.length()-2);
         
        browser.loadUrl(url);
    } 
 
    @Override 
    public void onDestroy() { 
      super.onDestroy(); 
Experience pair programming with AI  Get Codota for Java