BackPressCloseHandler.java

package kr.co.test;

import android.app.Activity;
import android.widget.Toast;

public class BackPressCloseHandler {

  private long backKeyPressedTime = 0;
  private Toast toast;
  private Activity activity;

  public BackPressCloseHandler(Activity context) {
    this.activity = context;
  }

  public void onBackPressed() {
    if (System.currentTimeMillis() > backKeyPressedTime + 2000) {
      backKeyPressedTime = System.currentTimeMillis();
      showGuide();
      return;
    }
    if (System.currentTimeMillis() <= backKeyPressedTime + 2000) {
      activity.finish();
      toast.cancel();
    }
  }

  public void showGuide() {
    toast = Toast.makeText(activity, "뒤로 버튼을 한번 더 누르시면 종료됩니다.", Toast.LENGTH_SHORT);
    toast.show();
  }

}


MainActivity.java

package kr.co.test;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

  private static final String URL = "https://www.google.co.kr/";
  private WebView webView;
  private BackPressCloseHandler backPressCloseHandler = new BackPressCloseHandler(this);

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    webView = new WebView(this);
    setContentView(webView);
    webView.setWebViewClient(new WebViewClient());
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webView.loadUrl(URL);
  }

  @Override
  public void onBackPressed() {
    if (webView != null && webView.getOriginalUrl().equalsIgnoreCase(URL)) {
      backPressCloseHandler.onBackPressed();
    } else if (webView != null && webView.canGoBack()) {
      webView.goBack();
    } else {
      backPressCloseHandler.onBackPressed();
    }
  }

}

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="kr.co.test">

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:networkSecurityConfig="@xml/network_security_config"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">
    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
  <uses-permission android:name="android.permission.INTERNET" />
</manifest>

 

res/layout/activity_main.xml

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

 

res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">www.google.co.kr</domain>
  </domain-config>
</network-security-config>

 

'컴퓨터 > Android' 카테고리의 다른 글

Android 간단한 웹뷰 처리 및 Back Button 수정  (0) 2020.08.30

+ Recent posts