¾Èµå·ÎÀ̵å > Webview - WebViewClientÀÇ ¸Þ¼µå
¸ÕÀú WebViewÀÇ ¸¸µå´Â ¹æ¹ýÀ» ¾Ë¾Æ ºÃ´Ù.
±Ùµ¥ WebView¸¦ ¸¸µçµÚ¿¡ È®ÀÎÇØ º¸¸é WebView¿¡¼ »õ·Î¿î Url Á¢¼ÓÀÌ ÀÏ¾î ³¯¸é
»õ·Î¿î âÀÌ ¶ß´Â°É º¼ ¼ö ÀÖ´Ù. ÀÌ´Â ±âº»ÀûÀ¸·Î WebView¿¡¼ »õ·Î¿î Url Á¢¼Ó½Ã
½Ã½ºÅÛ¿¡¼ »õ·Î¿î â¿¡´Ù°¡ ·ÎµùÇϵµ·Ï ½ÃÅ°´Â °ÍÀε¥, ÀÌ°ÍÀÌ Çѵιø¾¿ ¸¹¾ÆÁö¸é
¸Þ¸ð¸® °ü¸®»ó¿¡µµ º°·Î µµ¿òÀÌ ¾ÈµÈ´Ù. ±×·³ ¾îÂîÇØ¾ß µÉ±î..
±×·¡¼ ÀÖ´Â °ÍÀÌ WebViewClient ¿Í WebChromeClient ÀÌ´Ù.
À̹ø Àå¿¡´Â webViewClient class ¿¡ ´ëÇؼ ¾Ë¾Æº¸ÀÚ.
ÀÏ´Ü WebViewClient ´Â »õ·Î¿î Class¿¡ WebViewClient¸¦ »ó¼Ó ¹Þ¾Æ¼ ¸¸µé¾îµµ µÇ°í
private class WebClientClass extends WebViewClient {
~~~~~~ ³»¿ë ~~~~~~~~~~~~~~~~~~~~~~
}
³»ºÎ¿¡¼ new·Î »ý¼ºÇصµ µÈ´Ù.
WebView.setWebViewClient(new WebViewClient(){
~~~~~~ ³»¿ë ~~~~~~~~~~~~~~~~~~~~~~
}
WebViewClient Class ÇÔ¼öµé¿¡ ´ëÇؼ ¾Ë¾Æº¸ÀÚ.
1. onPageStarted()
·ÎµùÀÌ ½ÃÀ۵ɶ§..
WebView¿¡¼ óÀ½ ÇÑ ¹ø¸¸ È£ÃâµÇ´Â ¸Þ½îµå ÆäÀÌÁö ·ÎµùÀÌ ½ÃÀÛµÈ °ÍÀ» ¾Ë¸³´Ï´Ù.
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
2. onLoadResource()
WebView°¡ ÁÖ¾îÁø URL·Î ÁöÁ¤µÈ ¸®¼Ò½º¸¦ ·ÎµåÇÒ °ÍÀ̶ó°í ¾Ë¸³´Ï´Ù.
ÆäÀÌÁö ·ÎµùÀÌ ¿Ï·áµÉ ¶§±îÁö ¿©·¯¹ø È£ÃâµË´Ï´Ù. ÆäÀÌÁö°¡ ³ª´µ¾î¼ ·ÎµùµÇ³ª º¾´Ï´Ù.
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
3. doUpdateVisitedHistory()
¹æ¹®ÇÑ ¸µÅ©¸¦ µ¥ÀÌÅͺ£À̽º¿¡ ¾÷µ¥ÀÌÆ®ÇÑ´Ù°í ¾Ë¸³´Ï´Ù.
@Override
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
Log.i("WebView", "History: " + url );
super.doUpdateVisitedHistory(view, url, isReload);
}
/* °á°ú (urlÀÌ º¯ÇÒ ¶§ ¸¶´Ù)
History: http://www.abc.com/djfk...
History: http://www.abc.com/djfk.../fdfd
*/
4. onPageFinished()
WebView¿¡¼ óÀ½ ÇÑ ¹ø¸¸ È£ÃâµÇ´Â ¸Þ½îµå. ÆäÀÌÁö ·ÎµùÀÌ ¿Ï·áµÈ °ÍÀ» ¾Ë¸³´Ï´Ù.
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
5. onReceivedError()
È£½ºÆ® ÀÀ¿ë ÇÁ·Î±×·¥¿¡°Ô ¿À·ù¸¦ º¸°íÇÕ´Ï´Ù. ÀÌ·¯ÇÑ ¿À·ù´Â º¹±¸ÇÒ ¼ö ¾ø½À´Ï´Ù.
À¥ºä´Â ÀÎÅͳÝÀÌ ¿°áµÇ¾î ÀÖÁö ¾Ê¾ÒÀ»¶§ ÁÖ¼Ò°¡ ³ëÃâµÇ´Â ´ÜÁ¡ÀÌ ÀÖ´Ù. ÀÌ·²°æ¿ì urlÁÖ¼Ò¸¦ º¸¾È»ó ³ëÃâµÇ¸é ¾ÈµÇ±â ¶§¹®¿¡ ¼û±æ°æ¿ì »ç¿ëÇϸé À¯¿ëÇÒ °Í °°´Ù.
@Override
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
switch(errorCode) {
case ERROR_AUTHENTICATION: break; // ¼¹ö¿¡¼ »ç¿ëÀÚ ÀÎÁõ ½ÇÆÐ
case ERROR_BAD_URL: break; // À߸øµÈ URL
case ERROR_CONNECT: break; // ¼¹ö·Î ¿¬°á ½ÇÆÐ
case ERROR_FAILED_SSL_HANDSHAKE: break; // SSL handshake ¼öÇà ½ÇÆÐ
case ERROR_FILE: break; // ÀÏ¹Ý ÆÄÀÏ ¿À·ù
case ERROR_FILE_NOT_FOUND: break; // ÆÄÀÏÀ» ãÀ» ¼ö ¾ø½À´Ï´Ù
case ERROR_HOST_LOOKUP: break; // ¼¹ö ¶Ç´Â ÇÁ·Ï½Ã È£½ºÆ® À̸§ Á¶È¸ ½ÇÆÐ
case ERROR_IO: break; // ¼¹ö¿¡¼ Àаųª ¼¹ö·Î ¾²±â ½ÇÆÐ
case ERROR_PROXY_AUTHENTICATION: break; // ÇÁ·Ï½Ã¿¡¼ »ç¿ëÀÚ ÀÎÁõ ½ÇÆÐ
case ERROR_REDIRECT_LOOP: break; // ³Ê¹« ¸¹Àº ¸®µð·º¼Ç
case ERROR_TIMEOUT: break; // ¿¬°á ½Ã°£ ÃÊ°ú
case ERROR_TOO_MANY_REQUESTS: break; // ÆäÀÌÁö ·ÎµåÁß ³Ê¹« ¸¹Àº ¿äû ¹ß»ý
case ERROR_UNKNOWN: break; // ÀÏ¹Ý ¿À·ù
case ERROR_UNSUPPORTED_AUTH_SCHEME: break; // Áö¿øµÇÁö ¾Ê´Â ÀÎÁõ ü°è
case ERROR_UNSUPPORTED_SCHEME: break; // URI°¡ Áö¿øµÇÁö ¾Ê´Â ¹æ½Ä
}
}
6. onReceivedHttpAuthRequest()
* ÀÎÁõ ¿äûÀ» ó¸®ÇÑ´Ù°í ¾Ë¸³´Ï´Ù. ±âº» µ¿ÀÛÀº ¿äûÀ» Ãë¼ÒÇÏ´Â °ÍÀÔ´Ï´Ù. (http ÀÎÁõ¿äûÀÌ ÀÖÀ» °æ¿ì)
@Override
public void onReceivedHttpAuthRequest(WebView view,HttpAuthHandler handler, String host, String realm) {
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
6. onScaleChanged()
½ºÄÉÀÏÀÌ º¯°æµÇ¾úÀ» ¶§ ó¸®ÇÒ ³»¿ëÀ» ±¸ÇöÇÑ´Ù. (È®´ë³ª Å©±âµîÀÌ º¯È ÀÖÀ» °æ¿ì)
@Override
public void onScaleChanged(WebView view, float oldScale, float newScale) {
super.onScaleChanged(view, oldScale, newScale);
}
7. onUnhandledKeyEvent()
À߸øµÈ Å° ÀÔ·ÂÀÌ ÀÖÀ» °æ¿ì È£ÃâµÇ´Â ¸Þ½îµå
½Ã½ºÅÛ Å°¸¦ Á¦¿ÜÇÏ°í, WebView´Â shouldOverrideKeyEvent°¡ true¸¦ ¹ÝȯÇÏ´Â °æ¿ì³ª
ÀϹÝÀûÀÎ flow¿¡¼ Ç×»ó Å° À̺¥Æ®¸¦ ó¸®ÇÕ´Ï´Ù. Å° À̺¥Æ®°¡ ¹ß»ýµÈ °÷À¸·Î ºÎÅÍ
ºñµ¿±âÀûÀ¸·Î È£ÃâµË´Ï´Ù.
@Override
public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
super.onUnhandledKeyEvent(view, event);
}
8. shouldOverrideKeyEvent()
»ç¿ëÀÚÀÇ Å° ÀÔ·ÂÀÌ ÀÖÀ» °æ¿ì È£ÃâµÇ´Â ¸Þ½îµå
½Ã½ºÅÛ Å°¸¦ Á¦¿ÜÇÏ°í, WebView´Â shouldOverrideKeyEvent°¡ true¸¦ ¹ÝȯÇÏ´Â °æ¿ì³ª
ÀϹÝÀûÀÎ flow¿¡¼ Ç×»ó Å° À̺¥Æ®¸¦ ó¸®ÇÕ´Ï´Ù. Å° À̺¥Æ®°¡ ¹ß»ýµÈ °÷À¸·Î ºÎÅÍ
ºñµ¿±âÀûÀ¸·Î È£ÃâµË´Ï´Ù.
@Override
public void shouldOverrideKeyEvent(WebView view, KeyEvent event) {
return super.shouldOverrideKeyEvent(view, event);
}
(¿¹Á¦) shouldOverrideKeyEvent()
------------------------------------------------------------------------
@Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
int keyCode = event.getKeyCode();
Log.d("HelloWebViewClient","#### shouldOverrideKeyEvent() " + keyCode);
if ((keyCode == KeyEvent.KEYCODE_DPAD_LEFT) && webview.canGoBack()) {
webview.goBack(); //Ãë¼Ò¹öÆ°½Ã Àü ÆäÀÌÁö·Î °£´Ù.
return true;
}else if ((keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) && webview.canGoForward()) {
webview.goForward(); //¿À¸¥ÂÊ ¹öÆ°½Ã ¾Õ ÆäÀÌÁö·Î °£´Ù.
return true;
}
return false;
}
9. shouldOverrideUrlLoading()
»õ·Î¿î URLÀÌ ÇöÀç WebView¿¡ ·ÎµåµÇ·Á°í ÇÒ ¶§ È£½ºÆ® ÀÀ¿ë ÇÁ·Î±×·¥¿¡°Ô ÄÁÆ®·ÑÀ» ´ë½ÅÇÒ ±âȸ¸¦ ÁÝ´Ï´Ù
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//return super.shouldOverrideUrlLoading(view, url);
view.loadUrl(url);
return true;
}