简体   繁体   中英

Android WebView page is note available

I have an android application. The application uses webview. How to change display : page not available" to my image. I'm a newbie, attaching my code. I need to change the "page not available" webview element to my own. I'm a newbie, don't scold me too much. Thanks!

enter code here

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.annotation.TargetApi;

public class MainActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ConnectivityManager cm = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();

        if (networkInfo.isConnected()) {
            webView = new WebView(this);
            webView.getSettings().setJavaScriptEnabled(true);

            final Activity activity = this;

            webView.setWebViewClient(new WebViewClient() {
                @SuppressWarnings("deprecation")
                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(activity, description, Toast.LENGTH_SHORT);
                }
                @TargetApi(android.os.Build.VERSION_CODES.M)
                @Override
                public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
                    // Redirect to deprecated method, so you can use it in all SDK versions
                    onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
                }
            });
            webView.setOnKeyListener(new View.OnKeyListener()
            {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event)
                {
                    if(event.getAction() == KeyEvent.ACTION_DOWN)
                    {
                        WebView webView = (WebView) v;

                        switch(keyCode)
                        {
                            case KeyEvent.KEYCODE_BACK:
                                if(webView.canGoBack())
                                {
                                    webView.goBack();
                                    return true;
                                }
                                break;
                        }
                    }

                    return false;
                }
            });

            if (networkInfo.isConnected()) {
            webView.loadUrl("https://saluut.ru/");

            setContentView(webView);}
            else {
                // Показываем текст:
                webView.loadData("Извините, интернета нет, вот мое сообщение/пожелание", "text/html", "utf-8");
                //Либо кормим подготовленную страницу:
                //webView.loadUrl("file:///android_asset/myPage.html");
            }

        }
        else {
            // Показываем текст:
            webView.loadData("Извините, интернета нет, вот мое сообщение/пожелание", "text/html", "utf-8");
            //Либо кормим подготовленную страницу:
            //webView.loadUrl("file:///android_asset/myPage.html");
        }
}}

enter code here

Can you suggest simpler methods to replace a given WebView element

create a custom error page and store it inside the asset folder. use the code below to load it

webView.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        mWebView.loadUrl("file:///android_asset/errorpage.html");
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM