Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


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