Android webView中调用JavaScript

原文地址:Android webView中调用JavaScript 作者:yyylinux

package com.samll.games.pilishaizi;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class PiLiShaiZiActivity extends Activity {
 WebView webView;
 Handler mHandler = new Handler();
 @Override
    public voidonCreate(Bundle savedInstanceState) {
   

       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       //实例化webView
       webView = (WebView)findViewById(R.id.webView1);
       //设置Web属性,能够执行JavaScript脚本
       WebSettings webSettings  =webView.getSettings();
       webSettings.setJavaScriptEnabled(true);
       //设置可以访问文件 
  webSettings.setAllowFileAccess(true);
  webView.setWebChromeClient(newWebChromeClient() {
   //处理javascript中的confirm
   @Override
   publicboolean onJsConfirm(WebView view, String url,
     Stringmessage, final JsResult result) {
    Builderbuilder = new Builder(PiLiShaiZiActivity.this);
    ;
    builder.setMessage(message);
    builder.setPositiveButton("确定",
      newAlertDialog.OnClickListener() {
       publicvoid onClick(DialogInterface dialog,
         intwhich) {
        result.confirm();
       }
      });
    builder.setNegativeButton("取消",
      newDialogInterface.OnClickListener() {
       publicvoid onClick(DialogInterface dialog,
         intwhich) {
        result.cancel();
       }
      });
    builder.setCancelable(false);
    builder.create();
    builder.show();

    returntrue;
   }
  });

     //设置默认缩放方式尺寸是far
       webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
       //支持缩放
       webSettings.setSupportZoom(true);
       // 缩放按钮
      // webSettings.setBuiltInZoomControls(true);
       //webView.setInitialScale(-1);//为25%,最小缩放等级

 

       //加载Url内容
       webView.loadUrl("file:///android_asset/pilishaizi/index.htm");
       //设置web视图客户端
       webView.setWebViewClient(new HelloWebViewClient());
       
 }
 
 //设置回退
 @Override
 public boolean onKeyDown(int keyCode, KeyEventevent) {
  if(keyCode==KeyEvent.KEYCODE_BACK){
   //弹出确定退出对话框
   newAlertDialog.Builder(this).setTitle("退出").setMessage("确定退出霹雳骰子游戏吗?")
   .setPositiveButton("确定",new OkListener(this))
   .setNegativeButton("取消",new CancelListener(this)).show();
   returntrue;
  }
  return super.onKeyDown(keyCode,event);
 }
 
 //Web视图客户端
 private class HelloWebViewClient extendsWebViewClient{
  @Override
  public booleanshouldOverrideUrlLoading(WebView view, String url) {
   // TODOAuto-generated method stub
   view.loadUrl(url);
   returntrue;
  }
 }
    protectedvoid onDestroy() { 
       super.onDestroy();           
       System.exit(0); 
       //或者下面这种方式 
       //android.os.Process.killProcess(android.os.Process.myPid());  
    }
 

 
}

你可能感兴趣的:(Android webView中调用JavaScript)