Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android WebView onJsAlert onJsConfirm

android WebView onJsAlert onJsConfirm

編輯:關於Android編程

package com.html;

import com.sqlite.Dao.NcpRegisterDAO;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;


public class Register_protocolActivity extends Activity {
  

    private Handler mHandler = new Handler();
    private WebView mWebView;
    private NcpRegisterDAO dao = null;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //踩上一腳,記錄有了就知道復位的時候要去哪裡
        dao = new NcpRegisterDAO(Register_protocolActivity.this);
        String name = "MainActivity";
        dao.add_Stepped(name);
        
        //隱藏標題欄
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隱藏狀態欄
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //該ACTIVITY不需要layout設置,所以直接指向MainActivity的main
        setContentView(R.layout.protocol);
        //將WebView實例化
        mWebView = (WebView) this.findViewById(R.id.webView);
        //WebView打開權限
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        //WebView,SWF文件運行權限
        mWebView.getSettings().setPluginsEnabled(true);


        mWebView.addJavascriptInterface(new Action(), "ncp");  
     
       mWebView.loadUrl("file:///android_asset/protocol.html");

       
          
          // 創建WebViewChromeClient
               WebChromeClient wvcc = new WebChromeClient() {             
          // 處理Alert事件
             
             public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
              // 構建一個Builder來顯示網頁中的alert對話框
                             Builder builder = new Builder(Register_protocolActivity.this);
                                 builder.setTitle("計算1+2的值");
                                 builder.setMessage(message);
                                 builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
                                     @Override
                                     public void onClick(DialogInterface dialog, int which) {
                                         result.confirm();
                                     }
                                 });
                                builder.setCancelable(false);
                                 builder.create();
                                 builder.show();
                                 return true;
                             }
             
             
             // 處理Confirm事件
                     public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
                         Builder builder = new Builder(Register_protocolActivity.this);
                         builder.setTitle("退出確認");
                         builder.setMessage(message);
                         builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
                             public void onClick(DialogInterface dialog, int which) {
                                 //result.confirm();
                                // 從協義開始到發送注冊信息,這些步驟退出後直接清零復位表,不然如果非正常關機後再重啟會再次進入該頁
                                dao.Clear_NcpStep_Set0();
                                 finish();
                                 
                             }
         
                         });
                         builder.setNeutralButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
         
                          public void onClick(DialogInterface dialog, int which) {
                                 result.cancel();
                             }
         
                         });
                         builder.setCancelable(false);
                         builder.create();
                         builder.show();
                         return true;
                     }
             
          };
       // 設置setWebChromeClient對象
          mWebView.setWebChromeClient(wvcc);
       
       
       
    }
    
    final class Action {  
        
        Action() {  
        }  
         
        public void clickOnAndroid_register() {  
           mHandler.post(new Runnable() {  
                public void run() {  
                   // mWebView.loadUrl("javascript:wave()");            
                    Toast.makeText(getBaseContext(), "歡迎進入注冊!", Toast.LENGTH_SHORT).show();
                    //Intent intent = new Intent(MainActivity.this,Register.class);
                    Intent intent = new Intent(Register_protocolActivity.this,Register_IDcard.class);
                    Register_protocolActivity.this.startActivity(intent);
                      //設置切換動畫,從右邊進入,左邊退出
                    overridePendingTransition(R.xml.in_from_right, R.xml.out_to_left);
                    
                }  
            });  
        }   
        
        
        public void clickOnAndroid_quit() {  
            mHandler.post(new Runnable() {  
                 public void run() {  
                     finish();
                 }  
             });  
         }
    } 

     //禁止回退
    public void onBackPressed() {
               // TODO Auto-generated method stub
               return;
               //super.onBackPressed();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("onStop!!");
        finish();
        // The activity is no longer visible (it is now "stopped")
    }

}

 

分享到: 
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved