Code example for WebView: Displaying web pages

Methods: addJavascriptInterfacegetSettingsloadUrlsetWebChromeClientsetWebViewClient, setOnKeyListener, setVisibility

0
    } 
 
    // 初始化 
    private void setupViews() { 
        mWebView = (WebView) findViewById(R.id.webview);
        WebSettings mWebSettings = mWebView.getSettings();
        // 加上这句话才能使用javascript方法 
        mWebSettings.setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginState(PluginState.ON);
        mWebView.getSettings().setPluginsEnabled(true);
        mWebView.setVisibility(View.VISIBLE);
 
        // 增加接口方法,让html页面调用 
        mWebView.addJavascriptInterface(new Object() {
            // 这里我定义了一个打开地图应用的方法 
            public void startMap() { 
                // Intent mIntent = new Intent(); 
                // ComponentName component = new ComponentName( 
                // "com.google.android.apps.maps", 
                // "com.google.android.maps.MapsActivity"); 
                // mIntent.setComponent(component); 
                // startActivity(mIntent); 
                String url = "http://static.jifenzhong.com/resources/video/m/2012/20120730/50_20120730092638_27308.mp4";
                Intent it = new Intent(Intent.ACTION_VIEW);
                it.setDataAndType(Uri.parse(url), "video/mp4;video/x-msvideo");
                startActivity(it);
                Toast.makeText(getApplicationContext(), "test", 1000).show();
            } 
        }, "demo"); 
        // 加载页面 
        // mWebView.loadUrl("file:///android_asset/demo1.html"); 
        // mWebView.loadUrl("http://172.16.3.35:8080/hudson/demo.html"); 
        mWebView.loadUrl("http://m.jifenzhong.com/");
        mWebView.setWebViewClient(new WebViewClientEmb());
        mWebView.setWebChromeClient(new WebChromeClient() {
            // 当WebView进度改变时更新窗口进度 
            @Override 
            public void onProgressChanged(WebView view, int newProgress) {
                // Activity的进度范围在0到10000之间,所以这里要乘以100 
                MainActivity.this.setProgress(newProgress * 100);
            } 
        }); 
        // 点击后退按钮,让WebView后退一页(也可以覆写Activity的onKeyDown方法) 
        mWebView.setOnKeyListener(new View.OnKeyListener() {
            @Override 
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
                        mWebView.goBack(); // 后退
Contextual code suggestions in your IDE  Get Codota for Java