From 4ceebd34d34649a97beaee7c50305db254c441c9 Mon Sep 17 00:00:00 2001 From: slymax Date: Mon, 23 Mar 2020 23:38:54 +0100 Subject: [PATCH] open external links from local resources in browser --- README.md | 9 ++++----- app/src/main/assets/index.html | 2 +- app/src/main/java/com/example/app/MainActivity.java | 4 +--- app/src/main/java/com/example/app/MyWebViewClient.java | 4 +--- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1a3b98f..a83def3 100755 --- a/README.md +++ b/README.md @@ -8,24 +8,23 @@ This is a template project for Android Studio that allows you to create an andro If you want to create an app that displays the content of a remote website -1. uncomment lines **25** and **26** in `MainActivity.java` and replace `http://example.com` with your remote source +1. uncomment line **24** in `MainActivity.java` and replace `http://example.com` with your remote source ```java mWebView.loadUrl("https://example.com"); - mWebView.setWebViewClient(new MyWebViewClient()); ``` -2. open the `MyWebViewClient.java` file and replace `example.com` on line **14** with your custom hostname +2. open the `MyWebViewClient.java` file and replace `example.com` on line **13** with your custom hostname ```java - if (Objects.requireNonNull(Uri.parse(url).getHost()).endsWith(".example.com")) { + if (url.startsWith("file:") || uri.getHost() != null && uri.getHost().endsWith("example.com")) { ``` ### Using a local source If you want to create a local HTML5 android app -1. uncomment line **29** in `MainActivity.java` +1. uncomment line **27** in `MainActivity.java` ```java mWebView.loadUrl("file:///android_asset/index.html"); diff --git a/app/src/main/assets/index.html b/app/src/main/assets/index.html index 8c46509..5dac20e 100644 --- a/app/src/main/assets/index.html +++ b/app/src/main/assets/index.html @@ -8,4 +8,4 @@ REPLACE THIS FILE WITH YOUR OWN INDEX.HTML - \ No newline at end of file + diff --git a/app/src/main/java/com/example/app/MainActivity.java b/app/src/main/java/com/example/app/MainActivity.java index a9cf1c2..2ac45b1 100755 --- a/app/src/main/java/com/example/app/MainActivity.java +++ b/app/src/main/java/com/example/app/MainActivity.java @@ -5,7 +5,6 @@ import android.app.Activity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; -import android.webkit.WebViewClient; public class MainActivity extends Activity { @@ -17,13 +16,12 @@ public class MainActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = findViewById(R.id.activity_main_webview); - mWebView.setWebViewClient(new WebViewClient()); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); + mWebView.setWebViewClient(new MyWebViewClient()); // REMOTE RESOURCE // mWebView.loadUrl("https://example.com"); - // mWebView.setWebViewClient(new MyWebViewClient()); // LOCAL RESOURCE // mWebView.loadUrl("file:///android_asset/index.html"); diff --git a/app/src/main/java/com/example/app/MyWebViewClient.java b/app/src/main/java/com/example/app/MyWebViewClient.java index d4a7f7d..e32b5fb 100755 --- a/app/src/main/java/com/example/app/MyWebViewClient.java +++ b/app/src/main/java/com/example/app/MyWebViewClient.java @@ -5,16 +5,14 @@ import android.net.Uri; import android.webkit.WebView; import android.webkit.WebViewClient; -@SuppressWarnings("unused") class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Uri uri = Uri.parse(url); - if (uri.getHost() != null && uri.getHost().endsWith(".example.com")) { + if (url.startsWith("file:") || uri.getHost() != null && uri.getHost().endsWith("example.com")) { return false; } - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); view.getContext().startActivity(intent); return true;