본문 바로가기

study

안드로이드스튜디오 하이브리드 앱

bundel.gradle(module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "com.nuri.ywcayouth"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

 

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nuri.ywcayouth">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

AndroidManifest.xml 
기존 레이아웃 제거 Pallette 에서 Widgets 선택 후 webview선택해서 그리드에 넣어줌

MainActivate.java

package com.nuri.ywcayouth;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLDecoder;

public class MainActivity extends AppCompatActivity {

    WebView wView;
    private PackageManager packageManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        setContentView(R.layout.activity_main);
        wView = findViewById(R.id.web);
        initWebView();  //
    }
    private long backBtnTime = 0;

    @Override
    public void onBackPressed() {
        long curTime = System.currentTimeMillis();
        long gapTime = curTime - backBtnTime;
        if (wView.canGoBack()) {
            wView.goBack();
        } else if (0 <= gapTime && 2000 >= gapTime) {
            super.onBackPressed();
        } else {
            backBtnTime = curTime;
            Toast.makeText(this, "한번 더 누르면 종료됩니다.", Toast.LENGTH_SHORT).show();
        }


    }

    public <val> void initWebView(){
        // 1. 웹뷰클라이언트 연결 (로딩 시작/끝 받아오기)
        wView.setWebViewClient(new WebViewClient(){
            @Override                                   // 1) 로딩 시작
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                String URL1 = url.toString().substring(0,24);
                String TEST = "test URL ";
                Log.e(TEST,URL1);
                if(URL1.equals("https://ywca-youth.or.kr")||URL1=="https://ywca-youth.or.kr"){
                    super.onPageStarted(view, url, favicon);
                }

            }
            @Override                                   // 2) 로딩 끝
            public void onPageFinished(WebView view, String url) {
                String URL1 = url.toString().substring(0,24);
                String TEST = "test URL ";
                Log.e(TEST,URL1);

                if(URL1.equals("https://ywca-youth.or.kr")||URL1=="https://ywca-youth.or.kr"){
                    super.onPageFinished(view, url);
                }

            }
            @Override                                   // 3) 외부 브라우저가 아닌 웹뷰 자체에서 url 호출
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                String TEST = "test_____________";
                String URL1 = url.toString();
                TEST = "test url1으 값 :";
                Log.d(TEST, URL1);
                String URL2 = URL1.substring(0, 24);
                String result2 = URL1.substring(0, 7);
                String result3 = URL1.substring(0, 20);
                TEST = "test페이지 안내  ";
                Log.e(TEST, result3);
                String GOOGLE_PLAY_STORE_PREFIX = "market://details?id=";



                    if (url.startsWith("intent:")) {
                        if(url.contains("kakaolink://send")) {
                            Intent sharingIntent = new Intent(Intent.ACTION_SEND);

                            Toast.makeText(getApplicationContext(), "카카오톡을 실행 합니다.", Toast.LENGTH_LONG).show();
                            String urlPage = "https://ywca-youth.or.kr";
                            String url1 = url;
                            try {
                                url1 = URLDecoder.decode(url1, "UTF-8"); //인코딩할 문자, 케릭터셋("UTF-8")
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            try {
                                url1 = URLDecoder.decode(url1, "UTF-8"); //인코딩할 문자, 케릭터셋("UTF-8")
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            String PROTOCOL_START = "intent:";
                            String PROTOCOL_END = ";end;";
                            String PROTOCOL_INTENT = "#Intent;";
                            int customUrlStartIndex = PROTOCOL_START.length();
                            int customUrlEndIndex = url1.indexOf(PROTOCOL_INTENT);
                            String resultUrl = url1.substring(customUrlStartIndex, customUrlEndIndex);
                            //sharingIntent.putExtra(String.valueOf(Intent.URI_INTENT_SCHEME), resultUrl);
                            //sharingIntent.setPackage("com.kakao.talk");
                            //startActivity(sharingIntent);

                            try {
                                Intent intent = Intent.parseUri(resultUrl,Intent.URI_INTENT_SCHEME);
                                startActivity(intent);
                            } catch (URISyntaxException e) {
                                e.printStackTrace();
                            }

                        }

                        return true;
                    } else {
                        if (url.startsWith("https://ywca-youth.or.kr")) {
                            view.loadUrl(url);
                            return true;
                        } else {
                            if (url.startsWith("https://")) {
                                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                                startActivity(intent);
                                return true;
                            }
                        }
                    }
                    return true;
            }
        });
        // 2. WebSettings: 웹뷰의 각종 설정을 정할 수 있다.
        WebSettings ws = wView.getSettings();
        ws.setJavaScriptEnabled(true); // 자바스크립트 사용 허가
        // 3. 웹페이지 호출
        wView.loadUrl("https://ywca-youth.or.kr/");
    }
}