¾È±Ô °øºÎ¹æ

Android > Webview - WebChromeClientÀÇ ¸Þ¼­µå

µî·ÏÀÏ : 2017-07-06 17:44 Á¶È¸¼ö : 54,081


 ¸ÕÀú WebViewÀÇ ¸¸µå´Â ¹æ¹ýÀ» ¾Ë¾Æ ºÃ´Ù.

±Ùµ¥ WebView¸¦ ¸¸µçµÚ¿¡ È®ÀÎÇØ º¸¸é WebView¿¡¼­ »õ·Î¿î Url Á¢¼ÓÀÌ ÀÏ¾î ³¯¸é

»õ·Î¿î âÀÌ ¶ß´Â°É º¼ ¼ö ÀÖ´Ù. ÀÌ´Â ±âº»ÀûÀ¸·Î WebView¿¡¼­ »õ·Î¿î Url Á¢¼Ó½Ã

½Ã½ºÅÛ¿¡¼­ »õ·Î¿î â¿¡´Ù°¡ ·ÎµùÇϵµ·Ï ½ÃÅ°´Â °ÍÀε¥, ÀÌ°ÍÀÌ Çѵιø¾¿ ¸¹¾ÆÁö¸é

¸Þ¸ð¸® °ü¸®»ó¿¡µµ º°·Î µµ¿òÀÌ ¾ÈµÈ´Ù. ±×·³ ¾îÂîÇØ¾ß µÉ±î..

±×·¡¼­ ÀÖ´Â °ÍÀÌ WebViewClient ¿Í WebChromeClient ÀÌ´Ù.

À̹ø Àå¿¡´Â webViewClient class ¿¡ ´ëÇؼ­ ¾Ë¾Æº¸ÀÚ.

ÀÌÁ¦ WebChromeClient¸¦ ¾Ë¾Æ º¸¸é

WebChromeClient´Â ÇöÁ¦ ÆäÀÌÁö¿¡¼­ ÀϾ´Â ¾Ë¶÷µîÀ» ¾Ë·Á ÁÖ±â À§ÇÑ Äݹé ÀÎÅÍÆäÀ̽º¶ó°í ¼³¸íµÇ¾î ÀÖ½À´Ï´Ù.

»ç¿ëÇÏ´Â ¹æ¹ýÀº ¸¶Âù°¡ÁöÀÔ´Ï´Ù.

private class WebChromeClass extends WebChromeClient{
~~~~~~~~~~~~ ³»¿ë ~~~~~~~~~~~~~~~~~~~~~~
}

°ú ³»ºÎ¿¡¼­ new·Î »ý¼ºÇصµ µË´Ï´Ù.

WebView.setWebChromeClient(new WebChromeClient(){
~~~~~~~~~~~ ³»¿ë ~~~~~~~~~~~~~~~~~~~~~~
}


ÀÌ·¸°Ô ÇÏ½Ã¸é µË´Ï´Ù.

WebChromeClient Class ÇÔ¼öµé¿¡ ´ëÇؼ­ ¾Ë¾Æº¸ÀÚ.

1. onProgressChanged()

ÆäÀÌÁö¸¦ ·ÎµùÇÏ´Â ÇöÀç ÁøÇà »óȲÀ» ÀüÇØÁÝ´Ï´Ù.

ÇöÀç ÆäÀÌÁö ·Îµù ÁøÇà »óȲ, 0°ú 100 »çÀÌÀÇ Á¤¼ö·Î Ç¥Çö.(0% ~ 100%)
 
	@Override
	public void onProgressChanged(WebView view, int newProgress) {
		Log.i("WebView", "Progress: " + String.valueOf(newProgress)); 
		super.onProgressChanged(view, newProgress);
	}


(¿¹Á¦) onProgressChanged()

ÀÏ´Ü Progress Bar¸¦ Çϳª ¸¸µå½Å ÈÄ¿¡
 
	private ProgressBar web_progress;
	web_progress = (ProgressBar)findViewById(R.id.progress);
	web_progress.setVisibility(View.GONE);  //È­¸é¿¡¼­ ¼û±è´Ï´Ù

	@Override
	public void onProgressChanged(WebView view, int newProgress){
		web_progress.setVisibility(View.VISIBLE); // È­¸é¿¡ º¸À̱â

		//ÁøÇà»óȲÀÌ 100%ÀÌ»óÀ̸é
		if(newProgress > 100){ 
			web_progress.setVisibility(View.GONE); //È­¸é¼­ °¨Ãß±â
		}
	}
	


ÀÌÁ¦ È­¸é¿¡¼­ Progress Bar°¡ ³ª¿Ô´Ù°¡ »ç¶óÁö´Â°É º¼ ¼ö ÀÖÀ»²®´Ï´Ù.^^

2. onReceivedIcon()

ÇöÀç ÆäÀÌÁö¿¡ »õ·Î¿î faviconÀÌ ÀÖ´Ù°í ¾Ë¸³´Ï´Ù.

icon ÇöÀç ÆäÀÌÁöÀÇ faviconÀÌ µé¾îÀÖ´Â ºñÆ®¸Ê 

faviconÀ̶õ: ÀϹÝÀûÀ¸·Î À¥ ºê¶ó¿ìÀúÀÇ URLÀÌ Ç¥½ÃµÇ±â Àü¿¡ ƯÁ¤ À¥»çÀÌÆ®¿Í °ü·ÃµÈ 16 × 16 Çȼ¿ ¾ÆÀÌÄÜ
 
	@Override
	public void onReceivedIcon(WebView view, Bitmap icon) {
		super.onReceivedIcon(view, icon);
	}
	



3. onReceivedTitle()

¹®¼­ Á¦¸ñ¿¡ º¯°æÀÌ ÀÖ´Ù°í ¾Ë¸³´Ï´Ù.
title  ¹®¼­ÀÇ »õ·Î¿î ŸÀÌƲÀÌ µé¾îÀÖ´Â ¹®ÀÚ¿­

/*  ¾Æ·¡Ã³·³ title ÅÂ±× »çÀÌÀÇ °ªÀ» °¡Á®¿É´Ï´Ù.
<title> LGÅÚ·¹ÄÞ ÀüÀÚ°áÁ¦ ¼­ºñ½º </title>
*/
 
	@Override
	public void onReceivedTitle(WebView view, String title) {
		super.onReceivedTitle(view, title);
	}
	



4. getDefaultVideoPoster()

¹®¼­ Á¦¸ñ¿¡ º¯°æÀÌ ÀÖ´Ù°í ¾Ë¸³´Ï´Ù.
title  ¹®¼­ÀÇ »õ·Î¿î ŸÀÌƲÀÌ µé¾îÀÖ´Â ¹®ÀÚ¿­

/*  ¾Æ·¡Ã³·³ title ÅÂ±× »çÀÌÀÇ °ªÀ» °¡Á®¿É´Ï´Ù.
<title> LGÅÚ·¹ÄÞ ÀüÀÚ°áÁ¦ ¼­ºñ½º </title>
*/
 
	@Override
	public void onReceivedTitle(WebView view, String title) {
		super.onReceivedTitle(view, title);
	}



5. onJsAlert()

ÀÚ¹Ù ½ºÅ©¸³Æ® °æ°í ´ëÈ­ »óÀÚ¸¦ µð½ºÇ÷¹ÀÌÇÑ´Ù°í Ŭ¶óÀ̾ðÆ®¿¡°Ô ¾Ë·ÁÁÝ´Ï´Ù.
Ŭ¶óÀ̾ðÆ®°¡ true¸¦ ¹ÝȯÇÒ °æ¿ì, WebView´Â Ŭ¶óÀ̾ðÆ®°¡ ´ëÈ­ »óÀÚ¸¦ ó¸®ÇÒ ¼ö ÀÖ´Ù°í ¿©±é´Ï´Ù.

Ŭ¶óÀ̾ðÆ®°¡ false¸¦ ¹ÝȯÇÒ °æ¿ì, WebView´Â ½ÇÇàÀ» °è¼ÓÇÕ´Ï´Ù.
 
	@Override
	public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
		return super.onJsAlert(view, url, message, result);
	}
	


(¿¹Á¦) onJsAlert() - AlertDialog »ý¼º
 
	@Override
	
	public boolean onJsAlert(WebView view, String url, String message, JsResult result) 
	{ 
		final JsResult finalRes = result;

		//AlertDialog »ý¼º
		new AlertDialog.Builder(view.getContext())
		.setMessage(message)
		.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				finalRes.confirm();  
			}
		})
		.setCancelable(false)
		.create()
		.show();
		return true;
	}                 
    

  



6. onJsBeforeUnload()

ÇöÀç ÆäÀÌÁö¿¡¼­ ³ª¿Í Ž»öÀ» È®Á¤ÇÏ´Â ´ëÈ­ »óÀÚ¸¦ µð½ºÇ÷¹ÀÌÇÑ´Ù°í Ŭ¶óÀ̾ðÆ®¿¡°Ô

¾Ë·ÁÁÝ´Ï´Ù. ÀÌ°ÍÀº ÀÚ¹Ù ½ºÅ©¸³Æ® À̺¥Æ® onbeforeunload()ÀÇ °á°úÀÔ´Ï´Ù. Ŭ¶óÀ̾ðÆ®°¡

true¸¦ ¹ÝȯÇÏ´Â °æ¿ì, WebView´Â Ŭ¶óÀ̾ðÆ®°¡ ´ëÈ­ »óÀÚ¸¦ ó¸®ÇÏ°í ÀûÀýÇÑ JsResult

¸Þ½îµå¸¦ È£ÃâÇÒ °ÍÀ̶ó°í ¿©±é´Ï´Ù. Ŭ¶óÀ̾ðÆ®°¡ false¸¦ ¹ÝȯÇÏ´Â °æ¿ì, trueÀÇ ±âº»°ªÀº

ÇöÀç ÆäÀÌÁö¿¡¼­ ³ª¿Í Ž»öÇϱ⸦ ¼ö¶ôÇϱâ À§ÇÑ ÀÚ¹Ù ½ºÅ©¸³Æ®¸¦ ¹ÝȯÇÏ°Ô µÉ °ÍÀÔ´Ï´Ù.

±âº» µ¿ÀÛÀº false¸¦ ¹ÝȯÇÏ´Â °ÍÀÔ´Ï´Ù. JsResult¸¦ true·Î ¼³Á¤ÇÑ °ÍÀº ÇöÀç ÆäÀÌÁö¿¡¼­ ³ª¿Í

Ž»öÇÒ °ÍÀÌ°í false·Î ¼³Á¤ÇÑ °ÍÀº Ž»öÀ» Ãë¼ÒÇÒ °ÍÀÔ´Ï´Ù.

Ŭ¶óÀ̾ðÆ®°¡ false¸¦ ¹ÝȯÇÒ °æ¿ì, WebView´Â ½ÇÇàÀ» °è¼ÓÇÕ´Ï´Ù.
 
	@Override
	public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) {
		return super.onJsBeforeUnload(view, url, message, result);
	}

	






7. onJsConfirm()

»ç¿ëÀÚ¿¡°Ô È®ÀÎ ´ëÈ­ »óÀÚ¸¦ µð½ºÇ÷¹ÀÌÇÑ´Ù°í Ŭ¶óÀ̾ðÆ®¿¡°Ô ¾Ë·ÁÁÝ´Ï´Ù. Ŭ¶óÀ̾ðÆ®°¡

true¸¦ ¹ÝȯÇÏ´Â °æ¿ì, WebView´Â Ŭ¶óÀ̾ðÆ®°¡ È®ÀÎ ´ëÈ­ »óÀÚ¸¦ ó¸®ÇÏ°í ÀûÀýÇÑ

JsResult ¸Þ½îµå¸¦ È£ÃâÇÒ ¼ö ÀÖ´Ù°í ¿©±é´Ï´Ù. Ŭ¶óÀ̾ðÆ®°¡ false¸¦ ¹ÝȯÇÏ´Â °æ¿ì falseÀÇ

±âº»°ªÀº ÀÚ¹Ù ½ºÅ©¸³Æ®·Î ¹ÝȯµÉ °Í ÀÔ´Ï´Ù. ±âº» µ¿ÀÛÀº false¸¦ ¹ÝȯÇÏ´Â °ÍÀÔ´Ï´Ù.
 
	@Override
	public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
		return super.onJsConfirm(view, url, message, result);
    }


(¿¹Á¦) onJsConfirm() - confirm â ¶ç¿ì´Â ¿¹Á¦.
 
	@Override
	public boolean onJsConfirm(WebView view, String url, String message, final android.webkit.JsResult result){
		
		new AlertDialog.Builder(view.getContext())
		.setTitle("ÁÁÀºµ¥...")
		.setMessage(message)
		.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				result.confirm();
			}
		})

		.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				result.cancel();
			}
		})
		
		.create()
		.show();

		return true;
	};


8. onJsPrompt()

»ç¿ëÀÚ¿¡°Ô ÇÁ·ÒÇÁÆ® ´ëÈ­ »óÀÚ¸¦ µð½ºÇ÷¹ÀÌÇÑ´Ù°í Ŭ¶óÀ̾ðÆ®¿¡°Ô ¾Ë·ÁÁÝ´Ï´Ù.

Ŭ¶óÀ̾ðÆ®°¡ true¸¦ ¹ÝȯÇÏ´Â °æ¿ì, WebView´Â Ŭ¶óÀ̾ðÆ®°¡ È®ÀÎ ´ëÈ­ »óÀÚ¸¦ ó¸®ÇÏ°í

ÀûÀýÇÑ JsResult ¸Þ½îµå¸¦ È£ÃâÇÒ ¼ö ÀÖ´Ù°í ¿©±é´Ï´Ù. Ŭ¶óÀ̾ðÆ®°¡ false¸¦ ¹ÝȯÇÏ´Â °æ¿ì

falseÀÇ ±âº»°ªÀº ÀÚ¹Ù ½ºÅ©¸³Æ®·Î ¹ÝȯµÉ °Í ÀÔ´Ï´Ù. ±âº» µ¿ÀÛÀº false¸¦ ¹ÝȯÇÏ´Â °ÍÀÔ´Ï´Ù.
 
	@Override
	public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
		return super.onJsPrompt(view, url, message, defaultValue, result);
    }



9. onJsTimeout()

ÀÚ¹Ù ½ºÅ©¸³Æ® ½ÇÇà Á¦ÇÑ ½Ã°£À» ÃÊ°úÇß´Ù°í Ŭ¶óÀ̾ðÆ®¿¡°Ô ¾Ë·ÁÁÝ´Ï´Ù. ±×¸®°í

Ŭ¶óÀ̾ðÆ®°¡ ½ÇÇàÀ» Áß´ÜÇÒÁö ¿©ºÎ¸¦ °áÁ¤ÇÒ ¼ö ÀÖ½À´Ï´Ù. Ŭ¶óÀ̾ðÆ®°¡ true¸¦ ¹ÝȯÇÏ´Â

°æ¿ì, ÀÚ¹Ù ½ºÅ©¸³Æ®°¡ Áߴܵ˴ϴÙ. Ŭ¶óÀ̾ðÆ®°¡ false¸¦ ¹ÝȯÇÏ´Â °æ¿ì, °è¼Ó ½ÇÇàµË´Ï´Ù.

Âü°í·Î Áö¼ÓÀûÀÎ ½ÇÇà »óÅ¿¡¼­´Â Á¦ÇÑ ½Ã°£ Ä«¿îÅÍ°¡ Àç¼³Á¤µÇ°í  ½ºÅ©¸³Æ®°¡ ´ÙÀ½ üũ

Æ÷ÀÎÆ®¿¡¼­ ¿Ï·áµÇÁö ¾ÊÀ» °æ¿ì °è¼Ó ÄݹéµÇ¾îÁú °ÍÁý´Ï´Ù.
 

	@Override
	public boolean onJsTimeout() {
		return super.onJsTimeout();
	}



10. openFileChooser()

À¥ÆäÀÌÁö»ó¿¡¼­ ÆÄÀÏ Ã·ºÎ¸¦ ÇØ¾ß ÇÒ °æ¿ì°¡ ÀÖ´Ù. ÀÌ°æ¿ì¿¡ »ç¿ëÇÏ´Â ¸Þ¼­µå ÀÌ´Ù.
 
	// For Android < 3.0
	public void openFileChooser( ValueCallback uploadMsg ){
		openFileChooser( uploadMsg, "" );
	}

	// For Android 3.0+
	public void openFileChooser(ValueCallback uploadMsg) {
		uploadMessage = uploadMsg;
		Intent i = new Intent(Intent.ACTION_GET_CONTENT);
		i.addCategory(Intent.CATEGORY_OPENABLE);
		//i.setType("*/*");
		i.setType("image/*"); 
		startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
	}

	// For Android 4.1+
	public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
		openFileChooser( uploadMsg, "" );
	}



¿©±â¼­ Áß¿äÇÑÁ¡Àº @Override ¸¦ ¾²¸é ¿À·ù³­´Ù´Â °ÍÀÌ´Ù.

¿Ö³ÄÇϸé openFileChooser ¸Þ¼Òµå´Â ÇÁ·¹ÀÓ¿÷¿¡¼­ @hidden 󸮵ǾîÀֱ⶧¹®¿¡ sdk ¿¡¼­ º¸ÀÌÁö ¾Ê±â ¶§¹®ÀÌ´Ù.

ÇÏ¿©Æ° Àξۺê¶ó¿ìÀú¿¡¼­ ÆÄÀÏ÷ºÎ¸¦ ÇÏ·Á°íÇϸé Àú ¸Þ¼Òµå°¡ ÀÚµ¿À¸·Î È£ÃâµÇ°Ô µÈ´Ù.



±âŸ ÇÔ¼öµé....
	@Override
	public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota, QuotaUpdater quotaUpdater) {
		super.onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota, quotaUpdater);
	}

	---------------------------------------------------------------------

	@Override
	public void onReceivedTouchIconUrl(WebView view, String url, boolean precomposed) {
		super.onReceivedTouchIconUrl(view, url, precomposed);
	}

	---------------------------------------------------------------------

	@Override
	public void onRequestFocus(WebView view) {
		super.onRequestFocus(view);
	}

	---------------------------------------------------------------------

	@Override
	public void onShowCustomView(View view, CustomViewCallback callback) {
		super.onShowCustomView(view, callback);
	}

	---------------------------------------------------------------------

	@Override
	public Bitmap getDefaultVideoPoster() {
		return super.getDefaultVideoPoster();
	}

	---------------------------------------------------------------------

	@Override
	public View getVideoLoadingProgressView() {
		return super.getVideoLoadingProgressView();
	}

	---------------------------------------------------------------------

	@Override
	public void getVisitedHistory(ValueCallback callback) {
		super.getVisitedHistory(callback);
	}

	---------------------------------------------------------------------

	@Override
	public void onCloseWindow(WebView window) {
		super.onCloseWindow(window);
	}

	---------------------------------------------------------------------

	@Override
	public void onConsoleMessage(String message, int lineNumber, String sourceID) {
		super.onConsoleMessage(message, lineNumber, sourceID);
	}

	---------------------------------------------------------------------

	@Override
	public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
		return super.onCreateWindow(view, dialog, userGesture, resultMsg);
	}

	---------------------------------------------------------------------

	@Override
	public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, 
			long totalUsedQuota, QuotaUpdater quotaUpdater) {
		super.onExceededDatabaseQuota(url, databaseIdentifier, currentQuota,
		estimatedSize, totalUsedQuota, quotaUpdater);
	}

	---------------------------------------------------------------------

	@Override
	public void onGeolocationPermissionsHidePrompt() {
		super.onGeolocationPermissionsHidePrompt();
	}

	---------------------------------------------------------------------

	@Override
	public void onGeolocationPermissionsShowPrompt(String origin,Callback callback) {
		super.onGeolocationPermissionsShowPrompt(origin, callback);
	}

	---------------------------------------------------------------------
	
	@Override
	public void onHideCustomView() {
		super.onHideCustomView();
	}



 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.