(4) [Android Studio] 네이티브 환경에서 외부 링크 통신하기

2023. 6. 24. 20:28Github 프로젝트/android입문

728x90
반응형
SMALL

※ 저장소 : https://github.com/BerkleyLim/android_project

 

GitHub - BerkleyLim/android_project: 이 프로젝트는 안드로이드 스튜디오 프로젝트를 입문하여 연습하기

이 프로젝트는 안드로이드 스튜디오 프로젝트를 입문하여 연습하기 위해 제작하였습니다. Contribute to BerkleyLim/android_project development by creating an account on GitHub.

github.com

 

 

이번 시간에는 지난시간에 이어서 Web View 와 Native 간 통신 하는 것을 다뤄보겠습니다.

Web View란 지난 시간의 내용과 같이 Html css js 등과 뛰우거나 http 혹은 https를 띄우는 역할을 하였습니다.

이번에는 url를 띄어봅니다.

 

MainActivity 적용

package com.example.webandnative;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;
    private static final String ENTRY_URL = "http://www.berkleylim.link/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.webview);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.loadUrl(ENTRY_URL);
    }
}

 

 

activity_main.xml 적용

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />


</androidx.constraintlayout.widget.ConstraintLayout>

 

 

res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 네트워크 보안 제한 (접근 허용 설정) -->
<network-security-config>
<!--        도메인 허용 되는 것, false 시 https 만 가능 -->
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">berkleylim.link</domain>
    </domain-config>
</network-security-config>

 

AndroidMainfext.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    >
<!--    package="com.example.webandnative"-->

    <uses-permission android:name="android.permission.INTERNET" />

<!--    networkSecurityConfig 설정 시 도메인 등록-->
    <application
        android:networkSecurityConfig="@xml/network_security_config"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.WebAndNative"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:usesCleartextTraffic="true"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

 

728x90
반응형
LIST