Last active
July 30, 2020 01:06
-
-
Save seongchan/752db643377f823950648d0bc80599c1 to your computer and use it in GitHub Desktop.
WebView 에서 임의 페이지가 계속 로딩되는 현상 피하기.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.clipandbooks.test.myapplication; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.res.Configuration; | |
import android.graphics.Bitmap; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.webkit.WebResourceError; | |
import android.webkit.WebResourceRequest; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
/** | |
* Created by 홍성찬 on 2017-03-08 | |
* WebView 페이지가 페이지 로딩하면서 깜박거리는 현상 수정하기 | |
* onPageStarted(), onPageFinished(). shouldOverrideUrlLoading() 내 코드 참고하세요. | |
*/ | |
public class WebPageLoadActity extends Activity { | |
private WebView webView; | |
private String TAG = "SEONGCHAN"; | |
@Override | |
public void onConfigurationChanged(Configuration newConfig){ | |
super.onConfigurationChanged(newConfig); | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_web); | |
Intent intent = getIntent(); | |
String goURL = intent.getExtras().getString("goURL"); | |
webView = (WebView) findViewById(R.id.webview); | |
webView.getSettings().setJavaScriptEnabled(true); | |
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); | |
webView.setWebViewClient(new WebViewClient() { | |
Boolean flag = false; | |
@Override | |
public void onPageStarted (WebView view, String url, Bitmap favicon){ | |
Log.d(TAG, "(WEBVIEW)onPageStarted : " + url); | |
super.onPageStarted(view, url, favicon); | |
flag = true; | |
} | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
super.onPageFinished(view, url); | |
Log.d(TAG, "(WEBVIEW)onPageFinished"); | |
flag = false; | |
} | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
int type = view.getHitTestResult().getType(); | |
Log.d(TAG, "(WEBVIEW)shouldOverrideUrlLoading : " + url); | |
Log.d(TAG, " - getURLLoding Type:" + type); | |
if (!flag) { | |
if (type > 0) { | |
Log.d(TAG, " - startLoading...:loadUrl called"); | |
view.loadUrl(url); | |
return true; | |
} else { | |
Log.d(TAG, " - startLoading...:loadUrl pass!!"); | |
return false; | |
} | |
} else { | |
Log.d(TAG, " - under Loading... (SKIP...) "); | |
return false; | |
} | |
} | |
@Override | |
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { | |
super.onReceivedError(view, request, error); | |
int errorCode = error.getErrorCode(); | |
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가 지원되지 않는 방식 | |
} | |
Log.d(TAG, "(WEBVIEW)onReceivedError : " + errorCode ); | |
} | |
}); | |
WebSettings set = webView.getSettings(); | |
set.setLoadWithOverviewMode(true); | |
set.setUseWideViewPort(true); | |
webView.loadUrl(goURL); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment