130 likes | 550 Views
Comp194-MA. Lecture 4: Embedded browsers. Motivation. There are several reasons why an application may want to have access to a web browser. Example: display HTML fragments, access external services, open Google search ect.
E N D
Comp194-MA Lecture 4:Embedded browsers
Motivation • There are several reasons why an application may want to have access to a web browser. • Example: display HTML fragments, access external services, open Google search ect. • Additionally, accessing HTTP resources without a web browser abstraction layer is tedious and unnecessary.
Android’s solution • Android provides two solutions to this issue: • Use an Intent to cause Android to launch its own system browser. • Embed a WebView controller inside an Activity • Both of these approaches have their own unique advantages and disadvantages.
Android intents • The Android intents are an abstraction that allow activities to easily launch other activities. • The platform ships with a dozen or so default intents. They allow things like launching a telephone call, viewing a contact, and of course opening the web browser.
Intents pros/cons • Launching an intent to handle browsing utilizes the built in Android browser which is more robust than WebView. • The built in browser comes with “helpers” like a status bar, loading bar, ect. • The major con is that the application looses control of the phone when the browser is launched. • There is no way to intercept events or force the user back into an application.
Using WebView • The WebView controller allows for HTML fragments and web pages to be displayed in an activity. • WebView interacts with an Android activity like any other widget. • As such, an activity can capture events and as well as interact with the DOM on the page. • Unfortunately, the WebView implementation does not support AJAX out of the box.
Intent Code Intent i = new Intent(); i.setAction("android.intent.action.VIEW"); i.addCategory("android.intent.category.BROWSABLE"); Uri uri = Uri.parse(fbClient.getLoginURL(authToken)); i.setData(uri); // set the URL you want to open here fb.startActivity(i); // fire the event // Your activity will continue running while the browser is open. // The user can return to your activity if they hit “back” on the phone’s hard buttons.
WebView Code Embed in a layout.xml file: <WebView android:id="@+id/webview" android:layout_width="fill_parent“ android:layout_height="0dip" android:layout_weight="1" /> To navigate to a page: WebView wv = (WebView) fb.findViewById(R.id.webview); wv.loadUrl(fbClient.getLoginURL(authToken)); wv.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; }});
WebViewgotcha’s • WebView has a lot of unintuitive default behavior. • For example: by default WebView will open links in a new Android browser window. • To override this, pass the WebView controller a WebViewClient with the right functions overridden.
WebView example • In the example code: public boolean shouldOverrideUrlLoading (WebView view, String url) { return false; } • Overriding shouldOverrideUrlLoading and returning false will cause WebView to load links.
Misc. • In order for WebView to access the internet your application needs the android.permission.INTERNET permission. • Network latency can be simulated using the DDMS console. • You probably want to set WebView to fill_parent since small browsers are no fun. • As of SDK 1.1 R1 WebView loads HTTP over SSL URLs particularly slowly.
Google docs • http://d.android.com/reference/android/content/Intent.html • http://developer.android.com/reference/android/webkit/WebView.html
The Project • This is a big one so we’ll break it into two weeks. • Goal: Use the Facebook API to load the photos for the contacts in the phone. • Issues: How to interface with Facebook? How to create a contact? How to download photos? (???)