1 / 13

Lecture 4: Embedded browsers

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.

Roberta
Télécharger la présentation

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Comp194-MA Lecture 4:Embedded browsers

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

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

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

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

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

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

  8. 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; }});

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

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

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

  12. Google docs • http://d.android.com/reference/android/content/Intent.html • http://developer.android.com/reference/android/webkit/WebView.html

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

More Related