简体   繁体   中英

android webview on no connection error

I am trying to show a custom error of no connection, but its not working below is the code and in the manifest.xml i have provided network status. Connection() is checking the network status in the class. Hope this helps in sorting out the problem.

package com.testing.webview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {
    public static final int DIALOG_NONETWORK = 0;
    WebView mWebView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if(!Connection()){
            Context context = getApplicationContext();
            CharSequence text = "Sorry you need an Internet connection!  Please try again when the network is available.";
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            finish();
        }

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("file:///android_asset/index.html");
        mWebView.setWebViewClient(new HelloWebViewClient());
        mWebView.setBackgroundColor(Color.TRANSPARENT);
    }


    public boolean Connection() { 
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
        if (netInfo != null && netInfo.isConnectedOrConnecting()){ 
            return true;
        }
        else{
            return false;
        } 
    }


    private class HelloWebViewClient extends WebViewClient {        

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

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

Why are you finishing the activity after there is no connection?

Have the Toast like:

Toast.makeText(getApplicationContext(),"Network is down",
                        Toast.LENGTH_SHORT).show();

And then make the view show some sort of Button to "Retry" the network request, that's what i would do anyway. The Toast isn't currently showing probably because the activity is closed straight away after the toast, so there isn't enough time to show it

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