Comp194 ma
1 / 13

Lecture 4: Embedded browsers - PowerPoint PPT Presentation

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.

Related searches for Lecture 4: Embedded browsers

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

Download Presentation

Lecture 4: Embedded browsers

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


Lecture 4:Embedded browsers


  • 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();



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(;


wv.setWebViewClient(new WebViewClient(){


public boolean shouldOverrideUrlLoading(WebView view, String url) {

return false;



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


  • 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



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? (???)

  • Login