Code example for WebView: Displaying web pages

Methods: canGoBackgetUrlgoBackloadUrl

0
 
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && isStartPage()) {
			// on quit application.. 
			// getMetrics here... 
			System.out.println("com.manuelmaly.mockingdroid.monitor: " + this.monitor.getMetrics(webview.getUrl()));
 
			if (this.lastBackPressTime < System.currentTimeMillis() - 2000) {
				backToast = Toast.makeText(this, "Press back again to end this session!", Toast.LENGTH_SHORT);
				backToast.show();
				this.lastBackPressTime = System.currentTimeMillis();
				return true; 
			} else { 
				if (backToast != null)
					backToast.cancel();
				return super.onKeyDown(keyCode, event); // let app handle back button (activity end)
			} 
		} else if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
			if (backButtonGloballyAllowed && currentPageBackButtonAllowed) {
				if (currentPageBackButtonURL != null) {
					String gotoURL = URIHelper.urlForLink(webview.getUrl(), currentPageBackButtonURL).toString();
					Log.i(TAG, "Backbutton-Advice says: go to: " + gotoURL);
					webview.loadUrl(gotoURL);
				} else { 
					webview.goBack();
				} 
				this.monitor.backButtonPressed(webview.getUrl());
			} 
			return true; 
		} 
		if ((keyCode == KeyEvent.KEYCODE_MENU)) {
			URI menuURL = URIHelper.getMenuScreenURL(getApplicationContext(), webview.getUrl());
			if (menuURL != null)
				webview.loadUrl(menuURL.toString());
			return true; 
		} 
		if ((keyCode == KeyEvent.KEYCODE_SEARCH)) {
			URI searchURL = URIHelper.getSearchScreenURL(getApplicationContext(), webview.getUrl());
			if (searchURL != null)
				webview.loadUrl(searchURL.toString());
			return true; 
		} 
 
		return super.onKeyDown(keyCode, event);
	} 
 
	private boolean isStartPage() { 
		return webview.getUrl().equals(startURI);
	} 
 
	private class JavaScriptInterface { 
		/** 
		 * Gets called by JS within the currently displayed HTML file, to enable