embeding the web browser in your app

advertisement
Cosc 4755
Blackberry and Android
Embedding the browser in your app
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”.
Browser object
• Android webkit
– android.webkit
• WebView, a simple but very powerful widget
• Blackberry
– net.rim.device.api.browser.field 4.0.0+
• powerful, but complex.
– Requires code signing to use outside the simulator
– net.rim.device.api.browser.field2 V5.0.0+
• BrowserField, simple but very powerful field
– does NOT require code signing.
ANDROID
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
•
<uses-permission
android:name="android.permission.INTERNET" />
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());
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
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.
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.
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.
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.
BLACKBERRY
BrowserField
• We are going to skip over browser.field and
use the browser.field2 package.
• To use the basics
– Declare the BrowserField and add it to the screen.
– BrowserField.requestContent(URL);
• The content of the webpage will be displayed on the
screen, using the whole screen
– Assuming it is the only field added.
Configuring.
• By default most things are turned on with the
browserfield.
– Use BrowserFieldConfig object to configure
BrowserFieldConfig config = new BrowserFieldConfig();
config.setProperty(BrowserFieldConfig.ALLOW_CS_XHR,
Boolean.TRUE); //default is false
config.setProperty(BrowserFieldConfig.JAVASCRIPT_ENAB
LED, Boolean.TRUE); //default is already true.
config.setProperty(BrowserFieldConfig.ENABLE_COOKIES,
Boolean.TRUE); //default is already true.
browser = new BrowserField(config);
BrowserField (2)
• You can use the following to control what is
displayed in the field
– go forward or back on page
• forward(), back()
– fresh()
– setZoomScale(float scale) and float
getZoomScale() to control how the page is
zoomed.
– A note, when browserfield has focus, the BB menu will allow
the user to zoom fully out, when it has been “zoomed in”.
BrowserField (3)
• BrowserFieldHistory getHistory()
– which returns the browser history of current field
• Document getDocuement()
– returns a Document object (xml), which is likely a
subinterface HTMLDocument object.
• getDocumentTitle() and getDocumentUrl()
– returns a String with the info
• BrowserFieldListener
– A listener how the page is “working”
BrowserFieldHistory
• Methods in the BrowserFieldHistory object
• boolean canGoBack()
– This method returns true iff there are pages backwards in the history
• goBack()
– This method will find the previous page loaded into this BrowserField instance
and reload it
•
boolean canGoForward()
– This method returns true iff there are pages forwards in the history
• goForward()
– This method will find the next page in this BrowserField instance's history and
reload it
• go(int distance)
– This method will find the page a certain distance forward or backwards from
the current page and load that pages
• clearHistory() and refresh()
BrowserFieldHistory Example
• Check the history to see if we can go back one
page
BrowserFieldHistory browserFieldHistory =
browser.getHistory();
if(browserFieldHistory.canGoBack()) {
//causes the bowserfield to back one.
browserFieldHistory.goBack();
}
• OR
browser.back() //goes back one page if it can.
BrowserFieldListener
• These are the methods can be overriding by extending
BrowserFieldLisetner
– documentAborted(BrowserField browserField, Document document)
– documentCreated(BrowserField browserField, ScriptEngine
scriptEngine, Document document)
– documentError(BrowserField browserField, Document document)
– documentLoaded(BrowserField browserField, Document document)
– documentUnloading(BrowserField browserField, Document
document)
– downloadProgress(BrowserField browserField, ContentReadEvent
event)
• Except there is no documentation about when/why they are called.
– DocumentLoaded is called when the BrowserField has finished loading
– DocumentError seems to be called when the document fails to load
Q&A
Download