2011年11月10日 星期四

[AndroidDev] WebView

[WebView]
  • The basis upon which you can do followings within your Activity:
    • Roll your own web browser.
    • Simply display some online content.
    • Uses the same rendering and JavaScript engine(WebKit) as the browser, but it runs under the control of your application.
  • By default:
    • A WebView provides no browser-like widgets.
    • No zoom in and out.
    • Does not enable JavaScript.
    • No navigation controls.
      • Need to handle the BACK button on the device
      • Overrides URL loading, it automatically accumulates a history of visited web pages.
    • Web page errors are ignored.
    • Perform text searches. (?)
    • No address bar.
    • As you click a link, by default WebView will ask Activity Manager to choose the proper handler for the url.
      • Instead of adding an intent filter to view web pages, you can override the WebViewClient class and enable this Activity to handle its own URL requests.

Load content
  • Can download content from the web.
  • Can come from local files stored in your assets directory.
  • Can even be dynamically generated by your application code.
  • ex:
webview.loadUrl("http://www.google.com.tw/");  

// or

String summary = "You scored 192 points.";
webview.loadData(summary, "text/html", null);


Note:

A WebView has several customization points where you can add your own behavior. These are:
  • Creating and setting a WebChromeClient subclass. 
    • This class is called when something that might impact a browser UI happens, ex:
      • Progress update.
      • JavaScript alerts are sent here.
  • Creating and setting a WebViewClient subclass.
    • It will be called when things happen that impact the rendering of the content, ex:
      • Errors or form submissions.
    • Will load any URL selected from this WebView into the same WebView.
    • Can intercept URL loading here (via shouldOverrideUrlLoading()).
      • 預設點擊頁面上的 link 會由系統處理,開啟相對應的 app。
        • ex: 若是 URL 則開啟系統 Browser。
      • 但若 override shouldOverrideUrlLoading()
        • 則能依照自己意思判斷 URL 決定要在 WebView 開啟或是在 Browser 中開啟。ex:
        • return true
          • The method has handled the URL and the event should not propagate.
            • ex: an Intent would be created that's handled by the Browser application
          • 表示會自己處理 url。
          • 因此若沒有任何處理動作,點擊後不會有反應。
        • return false
          • The current WebView handles the url
          • 仍由系統處理,但會開啟在當前的 WebView。
          • 因此若有設定自己的執行動作又 return false,則兩種執行動作都會被觸發。
      • 所以如果想要畫面都在 WebView 中開啟,則表示一定要 override shouldOverrideUrlLoading()。
  • Related JavaScript
    • Enabling JavaScript with setJavaScriptEnabled() (Modifying the WebSettings)
    • addJavascriptInterface(Object, String)
      • Adding JavaScript-to-Java interfaces.
      • Has security issue.
      • Bind Java objects into the WebView so they can be controlled from the web pages JavaScript.
        • ex: Your JavaScript code can call a method in your Android code to display a Dialog, instead of using JavaScript's alert() function.
Data
  • For obvious security reasons, your application has its own cache, cookie store etc.—it does not share the Browser application's data
  • Cookies are managed on a separate thread, so operations like index building don't block the UI thread. 
  • CookieSyncManager
    • Is used to synchronize the browser cookie store between RAM and permanent storage.
      • To get the best performance, browser cookies are saved in RAM. A separate thread saves the cookies between, driven by a timer.
    • Note that even sync() happens asynchronously, so don't do it just as your activity is shutting down.

Note
  • Must claim this permission in AndroidManifest.xml or you won't open page and do not get any exception.
    • <uses-permission android:name="android.permission.INTERNET">


Related
[AndroidLayout] WebView Performance


* Reference
- WebView | Android Developers
WebViewClient | Android Developers
- WebChromeClient
WebSettings | Android Developers
CookieSyncManager | Android Developers
Android Developers Blog: Using WebViews
Building Web Apps in WebView | Android Developers
- WebView 1 - 4 ***
- SOP - Same Origin Policy (Browser Security Policy)

沒有留言:

張貼留言