如何在没有互联网连接的情况下在webview中加载自定义错误页面?

时间:2017-11-16 12:18:46

标签: javascript android webview

我试图了解如何将自定义离线页面添加到我的webview应用程序中。该应用程序运行良好我只是觉得自定义离线页面看起来会好得多!我在网上看了一些答案,但他们似乎不再工作了:(。我希望有人可以提供帮助! 这是我的主要代码是(在android工作室制作):



package com.rizwan.yvsse;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.onesignal.OneSignal;


public class MainActivity extends AppCompatActivity {
    WebView webView;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();

        Intent intent = new Intent(MainActivity.this, splash_screen.class);
        startActivity(intent);

        progressBar= (ProgressBar)findViewById(R.id.progressBar);
        webView = (WebView) findViewById(R.id.webview);
        String url="https://yvsse.com/billing/";
        webView.loadUrl(url);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new HelloWebViewClient());


    }

    @Override
    public void onBackPressed() {

        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    MainActivity.this);

            // set title
            alertDialogBuilder.setTitle("Exit");

            // set dialog message
            alertDialogBuilder
                    .setMessage("Do you really want to exit?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, close
                            // current activity
                            MainActivity.this.finish();
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }
    }


    private class HelloWebViewClient extends WebViewClient {


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {

            webView.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            progressBar.setVisibility(view.GONE);
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

        }



    }
}




谢谢!

1 个答案:

答案 0 :(得分:0)

覆盖onReceivedError方法并检查其中的互联网连接,以确认因互联网连接而导致网址失败

        @Override 
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
               if(!isappOnline()) {
               webView.loadUrl("file:///android_asset/error.html");
               }
        } 

        private boolean isappOnline() {
            ConnectivityManager cm = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
            return cm.getActiveNetworkInfo() != null;
        }

还会在资源文件夹src/main/assets/

中添加您的后备页面