WebView

1. WebView

  The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout.

  It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does,

    by default, is show a web page.

 

2. Adding a WebView to Your Application

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WebView" />
    
 <WebView 
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");

    

3. Using JavaScript in WebView

  Once JavaScript is enabled, you can also create interfaces between your application code and your JavaScript code.

  For example, JavaScript code can call a method in your Android code to display a Dialog , instead of using JavaScript's alert() function.

  3.1 Enabling JavaScript

    retrieve WebSettings with getSettings(), then enable JavaScript with setJavaScriptEnabled().

WebView myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

  3.2 Binding JavaScript code to Android code

    <1>Create a html in the tomcat. only a button in this HTML

      

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Mirror.showToast(toast);
    }
</script>

    <2>create a class in the project

//WebAppInterface
package mirror.android.minibrowser; import android.content.Context; import android.webkit.JavascriptInterface; import android.widget.Toast;
public class WebAppInterface { Context mContext; public WebAppInterface(Context c) { mContext = c; } @JavascriptInterface // this annotation is necessary!!!! public void showToast(String tosat){ Toast.makeText(mContext, "JavaScriptInterface", Toast.LENGTH_LONG).show(); } }

    <3> use addJavascriptInterface to create an interface called Mirror for JavaScript running in the WebView

public class MiniBrowser extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mini_browser);
        
        WebView myWebView = (WebView)findViewById(R.id.webView);
        
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        
     //(object, interfacename that JavaScript can call to access the Android class) myWebView.addJavascriptInterface(
new WebAppInterface(this), "Mirror"); myWebView.loadUrl("http://192.168.1.111:8080/MiniWeb/minibrowser.html"); } }

      

    One more thing to say

      

      

    The object that is bound to your JavaScript runs in another thread and not in the thread in which it was constructed.

 

 

4. Handling Page Navigation

  When the user clicks a link from a web page in your WebView , the default behavior is for Android to launch an application that handles URLs

    However, you can override this behavior for your WebView, so links open within your WebView

myWebView.setWebViewClient(new MyWebViewClient());
 private class MyWebViewClient extends WebViewClient{

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(Uri.parse(url).getHost().equals("www.example.com")){return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }

  Navigating web page history

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

 

5.

 

 

 

    

posted @ 2014-11-29 16:10  Mirrorhanman  阅读(1121)  评论(0编辑  收藏  举报