Android webView中调用JavaS…

原文地址: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 void onCreate(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(new WebChromeClient() {
   // 处理javascript中的confirm
   @Override
   public boolean onJsConfirm(WebView view, String url,
     String message, final JsResult result) {
    Builder builder = new Builder(PiLiShaiZiActivity.this);
    ;
    builder.setMessage(message);
    builder.setPositiveButton("确定",
      new AlertDialog.OnClickListener() {
       public void onClick(DialogInterface dialog,
         int which) {
        result.confirm();
       }
      });
    builder.setNegativeButton("取消",
      new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,
         int which) {
        result.cancel();
       }
      });
    builder.setCancelable(false);
    builder.create();
    builder.show();

    return true;
   }
  });

      //设置默认缩放方式尺寸是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, KeyEvent event) {
  if(keyCode==KeyEvent.KEYCODE_BACK){
   //弹出确定退出对话框
   new AlertDialog.Builder(this).setTitle("退出").setMessage("确定退出霹雳骰子游戏吗?")
   .setPositiveButton("确定", new OkListener(this))
   .setNegativeButton("取消", new CancelListener(this)).show();
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }
 
 //Web视图客户端
 private class HelloWebViewClient extends WebViewClient{
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
   // TODO Auto-generated method stub
   view.loadUrl(url);
   return true;
  }
 }
    protected void onDestroy() { 
        super.onDestroy();           
        System.exit(0); 
        //或者下面这种方式 
        //android.os.Process.killProcess(android.os.Process.myPid());  
    }
 

 
}

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