Android中webview的使用

Android客户端的代码:、


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class WebViewActivity extends Activity {

 private WebView mWebView;
 private String url;
 private AlertDialog aDialog;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  this.getWindow().requestFeature(Window.FEATURE_PROGRESS);// 让窗体的上面显示进度条
  this.getWindow().getDecorView()
    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);// 隐藏导航按钮
  setContentView(R.layout.webview);

  mWebView = (WebView) findViewById(R.id.webView1);
  aDialog = new AlertDialog.Builder(this).create();
  Intent intent = getIntent();
  url = intent.getStringExtra("id");
  System.out.println("------WebViewActivity-------" + url);
  WebSettings mWebSettings = mWebView.getSettings();
  // 启用或禁止webview访问文件数据
  mWebSettings.setAllowFileAccess(true);
  // 是否显示网络图像
  mWebSettings.setBlockNetworkImage(true);
  // 设置是否支持缩放
  mWebSettings.setBuiltInZoomControls(true);
  // 设置缓冲的模式
  // mWebSettings.setCacheMode();
  // 设置默认的字体大小
  mWebSettings.setDefaultFontSize(5);
  // 设置在解码时使用的默认编码
  mWebSettings.setDefaultTextEncodingName("utf-8");
  // 设置是否支持JavaScript
  mWebSettings.setJavaScriptEnabled(true);
  // 设置用鼠标激活被选项
  mWebSettings.setLightTouchEnabled(true);
  // 设置是否支持变焦
  mWebSettings.setSupportZoom(true);
  mWebView.loadUrl(url);

  // 设置webViewClient
  mWebView.setWebViewClient(new WebViewClient() {
   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub
    view.loadUrl(url);
    return true;
   }

   @Override
   public void onPageStarted(WebView view, String url, Bitmap favicon) {
    // TODO Auto-generated method stub
    super.onPageStarted(view, url, favicon);
   }

   @Override
   public void onPageFinished(WebView view, String url) {
    // TODO Auto-generated method stub
    super.onPageFinished(view, url);
   }

   @Override
   public void onReceivedError(WebView view, int errorCode,
     String description, String failingUrl) {
    // TODO Auto-generated method stub
    super.onReceivedError(view, errorCode, description, failingUrl);
    Toast.makeText(WebViewActivity.this, "page load error..",
      Toast.LENGTH_SHORT).show();
    aDialog.setTitle("ERROR");
    aDialog.setMessage(description);
    aDialog.setButton("OK", new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub

     }
    });
    aDialog.show();
   }

   @Override
   public void onScaleChanged(WebView view, float oldScale,
     float newScale) {
    // TODO Auto-generated method stub
    super.onScaleChanged(view, oldScale, newScale);
   }

   @Override
   public void onReceivedSslError(WebView view,
     SslErrorHandler handler, SslError error) {
    // TODO Auto-generated method stub
    super.onReceivedSslError(view, handler, error);
    handler.cancel();
    handler.proceed();
   }
  });

  mWebView.setWebChromeClient(new WebChromeClient() {
   @Override
   public boolean onCreateWindow(WebView view, boolean isDialog,
     boolean isUserGesture, Message resultMsg) {
    // TODO Auto-generated method stub
    return super.onCreateWindow(view, isDialog, isUserGesture,
      resultMsg);
   }

   @Override
   public boolean onJsAlert(WebView view, String url, String message,
     JsResult result) {
    // TODO Auto-generated method stub
    return super.onJsAlert(view, url, message, result);
   }

   @Override
   public boolean onJsConfirm(WebView view, String url,
     String message, JsResult result) {
    // TODO Auto-generated method stub
    return super.onJsConfirm(view, url, message, result);
   }

   @Override
   public boolean onJsPrompt(WebView view, String url, String message,
     String defaultValue, JsPromptResult result) {
    // TODO Auto-generated method stub
    return super.onJsPrompt(view, url, message, defaultValue,
      result);
   }

   @Override
   public void onProgressChanged(WebView view, int newProgress) {
    // TODO Auto-generated method stub
    /*
     * WebViewActivity.this.getWindow().setFeatureInt(
     * Window.FEATURE_PROGRESS, newProgress * 100);
     */
    super.onProgressChanged(view, newProgress);
    setProgress(newProgress * 100);
   }

   @Override
   public void onReceivedIcon(WebView view, Bitmap icon) {
    // TODO Auto-generated method stub
    super.onReceivedIcon(view, icon);
   }

   @Override
   public void onReceivedTitle(WebView view, String title) {
    // TODO Auto-generated method stub
    WebViewActivity.this.setTitle(title);
    super.onReceivedTitle(view, title);
   }

   @Override
   public void onRequestFocus(WebView view) {
    // TODO Auto-generated method stub
    super.onRequestFocus(view);
   }
  });
 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  // TODO Auto-generated method stub
  if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
   // 返回前一个页面
   mWebView.goBack();
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

AndroidManifest.xml中加上访问网络的权限

<uses-permission android:name="android.permission.INTERNET" />

你可能感兴趣的:(Android中webview的使用)