차근차근/Android

[AnDrOiD]WebChromeClient 에서 alert

예쁜꽃이피었으면 2014. 7. 29. 00:43

http://freecatz.blogspot.kr/2013/10/androidwebchromeclient-alert.html

 

package com.example.androidexam07;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

 WebView webView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  
  // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  // 가로 고정은 AndroidManifest.xml 에서 설정 할 것.

  webView = (WebView) findViewById(R.id.webView);

  webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); // 캐쉬 사용 안함
  
  webView.getSettings().setJavaScriptEnabled(true); // 자바스크립트 사용 여부

  webView.getSettings().setSupportMultipleWindows(true); // 멀티플 윈도우

  /*
   * webView.getSettings().setSaveFormData(false); // 비밀번호저장 사용 안함
   * webView.getSettings().setSavePassword(false); // 폼 데이터 저장(자동완성) 사용 안함
   * webView.getSettings().setBuiltInZoomControls(false); // 멀티줌 사용 안함
   * webView.getSettings().setSupportZoom(false); // 줌 사용 안함
   *
   * webView.setVerticalScrollBarEnabled(false); // 세로 스크롤바 사용 안함
   * webView.setHorizontalScrollBarEnabled(false); // 가로 스크롤바 사용 안함
   */

  webView.setWebViewClient(new webViewClient());
  webView.setWebChromeClient(new webViewChromeClient());

  webView.loadUrl("http://192.168.0.16/helloWeb/test.jsp");

 }

 
 
 /**
  * 프로그램 종료시
  * */
 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  
  super.onDestroy();
  
  webView.clearCache(true); // 웹뷰 캐시 삭제
  webView.destroy();
  webView = null;
  
  clearWebViewCache(null); // 웹뷰 캐시 삭제(전체)
  
  android.os.Process.killProcess(android.os.Process.myPid()); // pid kill
 }

 
 /**
  *  웹뷰 의 캐시 삭제
  * */
 private void clearWebViewCache(java.io.File dir){
  if(dir == null){
   dir = getCacheDir();
  }
  if(dir == null){
   return;
  }
  
  java.io.File[] content = dir.listFiles();
  
  try{
   for(int i=0; i<content.length; i++){
    if(content[i].isDirectory()){
     clearWebViewCache(content[i]);
    }else{
     content[i].delete();
    }
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 /**
  * '뒤로가기' 하드웨어 버튼 눌렀을 경우
  * */
 @Override
 public void onBackPressed() {
  // TODO Auto-generated method stub

  if(webView.canGoBack()){
   webView.goBack();
  }else{
   Toast.makeText(getBaseContext(), "프로그램을 종료 합니다.", Toast.LENGTH_SHORT).show();
   super.onBackPressed();
  }
 }
 
 /**
  * WebViewClient 상속
  * */
 private class webViewClient extends WebViewClient {

  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
   // TODO Auto-generated method stub
   // return super.shouldOverrideUrlLoading(view, url);
   view.loadUrl(url);
   return true;
  }

 }
 
 
 /**
  * WebChromeClient 상속
  * */
 private class webViewChromeClient extends WebChromeClient {

  @Override
  public boolean onCreateWindow(WebView view, boolean dialog,
    boolean userGesture, Message resultMsg) {
   // TODO Auto-generated method stub
   return super.onCreateWindow(view, dialog, userGesture, resultMsg);
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  @Override
  public boolean onJsAlert(WebView view, String url, String message,
    final JsResult result) {

   // TODO Auto-generated method stub
   // return super.onJsAlert(view, url, message, result);
   new AlertDialog.Builder(view.getContext(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
     .setTitle("알림")
     .setMessage(message)
     .setPositiveButton(android.R.string.ok,
       new AlertDialog.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
         result.confirm();
        }
       }).setCancelable(false).create().show();
   return true;
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  @Override
  public boolean onJsConfirm(WebView view, String url, String message,
    final JsResult result) {
   // TODO Auto-generated method stub
   // return super.onJsConfirm(view, url, message, result);
   new AlertDialog.Builder(view.getContext(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
     .setTitle("알림")
     .setMessage(message)
     .setPositiveButton("예",
       new AlertDialog.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
         result.confirm();
        }
     })
     .setNegativeButton("아니오",
       new AlertDialog.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
         result.cancel();
        }
       }).setCancelable(false).create().show();
   return true;
  }
 }

}

 


반응형