Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
10
Android TheWebKit Browser
VictorMatos
ClevelandStateUniversity
Notesarebasedon:
TheBusyCoder'sGuidetoAndroidDevelopment Th B C d ' G id t A d id D l t byMarkL.Murphy Copyright20082009CommonsWare,LLC. ISBN:9780981678009 & AndroidDevelopers http://developer.android.com/index.html
WebKit Browser
InAndroidyoucanembedthebuiltinWebbrowserasawidget inyourownactivities,fordisplayingHTMLmaterialorperform g Internetbrowsing. TheAndroidbrowserisbasedonWebKit,thesameenginethat powersApple'sSafariWeb browser. AndroidusestheWebView widgettohostthebrowserspages ApplicationsusingtheWebView componentmustrequest INTERNET permission.
10/25/2009
WebKit Browser
BrowsingPower
ThebrowserwillaccesstheInternetthroughwhatevermeans areavailabletothatspecificdeviceatthepresenttime(WiFi, are available to that specific device at the present time (WiFi cellularnetwork,Bluetoothtetheredphone,etc.). TheWebKit renderingengineusedtodisplaywebpagesincludes methodsto 1. 2. 3. 4. 5. 6. navigateforwardandbackwardthroughahistory, g g y, zoominandout, performtextsearches, loaddata stoploadingand more.
3
WebKit Browser
Warning
InorderforyourActivitytoaccesstheInternetandloadwebpages inaWebView,youmustaddtheINTERNET permissionstoyour AndroidManifestfile:
<uses-permission android:name="android.permission.INTERNET" />
10/25/2009
WebKit Browser
Example:Asimplebrowsingexperience Letsgoeshopping
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:id="@+id/webkit" android:layout_width="fill_p y parent" android:layout_height="fill_parent" /> </LinearLayout>
WebKit Browser
Example:Asimplebrowsingexperience Letsgoeshopping
package cis493 demoui; cis493.demoui; import android.os.Bundle; import android.app.Activity; import android.webkit.WebView; public class AndDemoUI extends Activity { WebView browser; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); browser=(WebView)findViewById(R.id.webkit); browser.loadUrl("http://eBay.com"); browser.getSettings().setJavaScriptEnabled(true); } }
10/25/2009
WebKit Browser
Example:Asimplebrowsingexperience Letsgoeshopping Manifest
<?xml version="1 0" encoding="utf-8"?> version= 1.0 encoding= utf-8 ?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cis493.demoui" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".AndDemoUI" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" / i i i i i /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>
7
WebKit Browser
Warning
IfyousettheURLtoasitewhosepagesdependonJavascript you If you set the URL to a site whose pages depend on Javascript you mayseeanempty,whitescreen. Bydefault Javascript isturnedoff inWebView widgets. IfyouwanttoenableJavascript,call:
myWebView.setSettings().setJavaScriptEnabled(true);
ontheWebView instance.
Tobediscussedlaterinthischapter.
8
10/25/2009
WebKit Browser
Warning
UnderSDK1.6aWebView hasabuiltin OptionMenu
UsingGo option
UsingMoreoption
WebKit Browser
LoadingData.loadData()
YoumaydirectlyprovidetheHTMLtobedisplayedbythebrowser (a user manual for instance or the actual app interface created as HTML instead ausermanualforinstance,ortheactualappinterfacecreatedasHTMLinstead ofusingthenativeAndroidUIframework).
package cis493.demoui; import android.os.Bundle; import android.app.Activity; import android.webkit.WebView; public class AndDemoUI extends Activity { WebView browser; @Override @O id public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); browser=(WebView)findViewById(R.id.webkit); Usesamelayoutandmanifest ofpreviousexample
10/25/2009
WebKit Browser
BrowserCommands
ThereisnonavigationtoolbarwiththeWebView widget(savingspace). YoucouldsupplytheUIsuchasaMenu toexecutethefollowingoperations: reload() torefreshthecurrentlyviewedWebpage goBack() togobackonestepinthebrowserhistory,andcanGoBack()to determineifthereisanyhistorytotraceback goForward() togoforwardonestepinthebrowserhistory,and canGoForward()todetermineifthereisanyhistorytogoforwardto goBackOrForward() togobackwardsorforwardsinthebrowserhistory, wherenegative/positive numbersrepresentacountofstepstogo where negative/positive numbers represent a count of steps to go canGoBackOrForward()toseeifthebrowsercangobackwardsorforwards thestatednumberofsteps(followingthesamepositive/negative conventionasgoBackOrForward()) clearCache() toclearthebrowserresourcecacheandclearHistory() toclear thebrowsinghistory
11
WebKit Browser
Usingourrunningexample: browser.goBack(); g (); browser.goForward(); browser.goBackOrForward(-2); browser.goBackOrForward(+2); browser.canGoBack(); browser.canGoForward(); browser.canGoBackOrForward(-2); browser.canGoBackOrForward(+2); browser.clearCache(true); browser.clearHistory(); browser.stopLoading();
12
10/25/2009
WebKit Browser
Questions?
13