Comp194 ma
1 / 13

lecture 4: - PowerPoint PPT Presentation

  • Updated On :

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'lecture 4:' - Roberta

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Comp194 ma l.jpg


Lecture 4:Embedded browsers

Motivation l.jpg

  • 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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
Intent Code

Intent i = new Intent();



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 l.jpg
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(;


wv.setWebViewClient(new WebViewClient(){


public boolean shouldOverrideUrlLoading(WebView view, String url) {

return false;


Webview gotcha s l.jpg

  • 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 l.jpg
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.

Slide11 l.jpg

  • 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 l.jpg
Google docs



The project l.jpg
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? (???)