Android Studio, Webview 2019

반응형

2019 Webview

Android Studio 3.4.2

- MainActivity

package com.umcoop.*****;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.ActionBar;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static com.umcoop.*****.R.id.webView;


public class MainActivity extends AppCompatActivity {

    private WebView mWebView;
    private String mCurrentUrl;
    private String myUrl = "URL";

    Intent intent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // 웹뷰 셋팅
        mWebView = (WebView) findViewById(webView);
        mWebView.getSettings().setJavaScriptEnabled(true);

        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);

        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClientClass());

        mWebView.loadUrl("file:///android_asset/index.html");

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class WebViewClientClass extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String overrideUrl) {
            if (overrideUrl.startsWith("tel:") || overrideUrl.startsWith("sms:") || overrideUrl.startsWith("smsto:") || overrideUrl.startsWith("mms:") || overrideUrl.startsWith("mmsto:") || overrideUrl.startsWith("mailto:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(overrideUrl));
                startActivity(intent);
                return true;
            }
            view.loadUrl(overrideUrl);
            return true;
        }
    }
}

 

- activiy_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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="com.example.jerry.WebView.MainActivity">

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

</LinearLayout>

 

- AndroidManifest

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

 

Reference

 

- assets

 · File → New → Folder  Folder Assets

 

- Lunch Images, drawable

 · File → New → Images Asset

 · File → New → Vector Asset

 

 

반응형

이 글을 공유하기

댓글(0)

Designed by JB FACTORY