Download presentation
Presentation is loading. Please wait.
Published byConrad Silas Hill Modified over 8 years ago
1
Cosc 5/4730 Embedding the browser in your app
2
Embedding the browser For many applications, you may not nothing more then the web browser – But you want your “Company Name” on it – The company web server can now serve all the contain needed. Almost never have to update the app with new stuff. But the company web server, must serve the data that is in the “size of the screen”.
3
Browser object Android webkit – android.webkit WebView, a simple but very powerful widget
4
Android webkit In the simplest form – Put a WebView Widget in the layout – If you want javascript enabled mWebView. getSettings().setJavaScriptEnabled(true); – Now load a page mWebView.loadUrl("http://www.cs.uwyo.edu"); In androidManifest.xml, you need to request permission to use the internet. – Add this
5
Android webkit (2) How to handle everything else. – In the previous slide, if a use clicks on a link, the android browser will now open and you app is paused. To better control the browser you extend the WebViewClient class – Then add that to the WebView mWebView.setWebViewClient(new myWebViewClient());
6
WebViewClient This controls the functions and gives you app’s notifications about what is going on. – boolean shouldOverrideUrlLoading(WebView view, String url) Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. – return true, if handled, otherwise false. – onReceivedError(WebView view, int errorCode, String description, String failingUrl) Report an error to the host application. – Other that maybe useful onPageStarted, onPageFinished, onReceivedSslError, shouldOverrideKeyEvent, onUnhandledKeyEvent
7
WebViewClient Example private class CallBack extends WebViewClient { public boolean shouldOverrideUrlLoading(WebView view, String url) { browser.loadUrl(url); return true; } I’ve only overriding one method, since I wanted to prevent the web browser from launching.
8
WebView methods reload(), which refreshes the page goBack(), which goes back one step in the browser history – canGoBack() returns true if there is at least one step back in the history goForward(), which goes forward one step in the browser history – canGoForward(), returns true if you can. ZoomIn(), ZoomOut(), stopLoading(), clearcache() and clearHistory() just to name a few.
9
WebViewClient example 2 //continuing the example, we add a keylistener to handle the back button @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } We can also create buttons, for refresh, forward, and back. – Even a progress bar using the getProgress() method in WebView.
10
The Rest Cache – CacheManager and CacheManger.CacheResult gives you access in the browser cache system Cookies – CookieManager and CookieSyncManger gives you access to the cookies WebSettings – Gives you access to dozens of settings, including – To enable the built-in zoom, set WebSettings.setBuiltInZoomControls(boolean) – Change the UserAgentString – This is also where the javascript settings are as well. – Can use mWebView.getSettings() which returns a WebSettings object which can be used to control the settings.
11
Q A &
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.