728x90

안드로이드 WebView 를 이용하여 MDB 기반 Web 화면을 읽어서 처리하는 코드이다.

LG G5폰(안드로이드 6.0)에서는 잘 동작한다.

삼성 갤럭시 S10(안드로이드 9.0)에서 실행을 시켰더니 로그인 자체부터 막힌다.

구글링해보니 HTTPS 통신이어야만 정상 동작된다고 나온다.

HTTP 통신에서 동작하도록 하는 방법은 아래와 같이 코드를 수정해주면 된다.


AndroidManifest.xml 파일 수정사항

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:theme="@style/AppTheme.NoActionBar">


/res/xml/network_security_config.xml 추가

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>


로 해주거나, 아래와 같이 한줄을 추가해주라고 나온다.

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"


일단 여기까지 하면 로그인까지는 문제없이 잘 동작되더라.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:orientation="vertical"
        >
        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:adjustViewBounds="true"
            android:scaleType="fitStart"
            />

    </LinearLayout>

</LinearLayout>


WebView 는 아래와 같이 처리하면 삼성 갤럭시 S10 에서도 이상없이 동작되는 걸 확인했다.

    private void myWebView(){
        String web_url = getIntent().getExtras().getString("url");
        try {
            url = URLDecoder.decode(web_url,"UTF-8"); // target
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("web url : " + web_url);

        mWebView = (WebView) findViewById(R.id.webview); // Layout 와의 연결

        mWebSettings = mWebView.getSettings(); // 세부 세팅 등록
        mWebSettings.setJavaScriptEnabled(true); // 자바스크립트 사용 허용
        mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebSettings.setBuiltInZoomControls(false); // 화면 확대 축소 허용 여부
        mWebSettings.setDisplayZoomControls(false);
        mWebSettings.setSupportZoom(true); // 줌 사용 여부
        //mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); // 컨텐츠 사이즈 맞추기
        mWebSettings.setUserAgentString("Android");
        if(Build.VERSION.SDK_INT >= 16){
            mWebSettings.setAllowFileAccessFromFileURLs(true);
            mWebSettings.setAllowUniversalAccessFromFileURLs(true);
        }
        if(Build.VERSION.SDK_INT >= 21){
            mWebSettings.setMixedContentMode(mWebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel:")) {
                    Intent call_phone = new Intent(Intent.ACTION_CALL);
                    call_phone.setData(Uri.parse(url));
                    startActivity(call_phone); // 권한 설정은 Loing.java에서 처리했음
                } else if (url.startsWith("sms:")) {
                    Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
                    startActivity(i);
                } else if (url.startsWith("intent:")) {
                    try {
                        Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                        Intent existPackage = getPackageManager().getLaunchIntentForPackage(intent.getPackage());
                        if (existPackage != null) {
                            startActivity(intent);
                        } else {
                            Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                            marketIntent.setData(Uri.parse("market://details?id=" + intent.getPackage()));
                            startActivity(marketIntent);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    view.loadUrl(url);
                }
                return true;
            }

        });

        mWebView.loadUrl(url);
    }


내가 사용했던 build.gradle, webview 등 파일 일부를 첨부한다.


WebView.zip



블로그 이미지

Link2Me

,