如果没有互联网,则阻止加载WebView

时间:2016-04-07 13:09:28

标签: java webview loading

我编写了一个通过Imagebutton打开WebView的应用程序。如果没有互联网且应用程序启动,则会出现一个小弹出窗口,其中显示"没有互联网"。如果我使用Internet启动应用程序并且在使用WebView期间丢失了Internet,则非常难看的屏幕会显示错误。如果没有互联网,小型Toast应该打开或者Textfield with"抱歉你丢失了你的连接:/",谢谢!

如何防止我的Webview加载新的一面?

这是我的代码

public static boolean checkInternetConnection(Context context) {
        ConnectivityManager con_manager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (con_manager.getActiveNetworkInfo() != null
                && con_manager.getActiveNetworkInfo().isAvailable()
                && con_manager.getActiveNetworkInfo().isConnected()) {
            return true;
        } else {
            return false;
        }
    }
    private ImageButton Ilias_link;
    private ImageButton Lsf_link;
    private WebView mWebView;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        if (!MainActivity.checkInternetConnection(this)) {
            Toast.makeText(getApplicationContext(), "Du hast leider kein Internet", Toast.LENGTH_SHORT).show();
        } else {
            Ilias_link = (ImageButton) findViewById(R.id.ilias_link);
        mWebView = new WebView(this);
        Ilias_link.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mWebView.getSettings().setJavaScriptEnabled(true);
                mWebView.getSettings().setBuiltInZoomControls(true);
                mWebView.getSettings().setDisplayZoomControls(false);
                mWebView.getSettings().setSupportZoom(true);
                if (savedInstanceState == null)
                    mWebView.loadUrl("https://elearns02.fh-biberach.de/ilias3/login.php?target=&soap_pw=&ext_uid=&cookies=nocookies&client_id=HSBC&lang=de");
                    mWebView.setWebViewClient(new WebViewClient());
                    setContentView(mWebView);}
        });
            Lsf_link = (ImageButton) findViewById(R.id.lsf_link);
            mWebView = new WebView(this);
            Lsf_link.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    mWebView.getSettings().setJavaScriptEnabled(true);
                    mWebView.getSettings().setBuiltInZoomControls(true);
                    mWebView.getSettings().setDisplayZoomControls(false);
                    mWebView.getSettings().setSupportZoom(true);
                    if (savedInstanceState == null)
                        mWebView.loadUrl("https://lsf.fh-biberach.de/qisserver/rds?state=user&type=0");
                    mWebView.setWebViewClient(new WebViewClient());
                    setContentView(mWebView);{
                    }};


            })
            ;}
    ImageButton imageButton = (ImageButton)findViewById((R.id.Mensaplan));
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Comming soon ;)",Toast.LENGTH_LONG).show();
        }
    });}
    public void onPageFinished(WebView view, String url) {
        String javascript="javascript:document.getElementsByName('viewport')[0].setAttribute('content', 'initial-scale=1.0,maximum-scale=10.0');";
        view.loadUrl(javascript);
    }
    @Override
    protected void onSaveInstanceState(Bundle outState )
    {
        super.onSaveInstanceState(outState);
        mWebView.saveState(outState);
    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        mWebView.restoreState(savedInstanceState);
    }
    private static final int TIME_INTERVAL = 3000;
    private long mBackPressed;
    @Override
        public void onBackPressed() {
            if (mWebView.canGoBack()) {
                mWebView.goBack();
                return;
            } else
            { if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis())
            {
                super.onBackPressed();
                return;
            }
            else { Toast.makeText(getBaseContext(), "Tap back button in order to exit", Toast.LENGTH_SHORT).show(); }
                mBackPressed = System.currentTimeMillis();

            }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试这样做 - 可能会起作用。我做了一个项目并使用了它。

DetectConnection.java

public class DetectConnection {             
public static boolean checkInternetConnection(Context context) {   

ConnectivityManager con_manager = (ConnectivityManager) 
  context.getSystemService(Context.CONNECTIVITY_SERVICE);

if (con_manager.getActiveNetworkInfo() != null
    && con_manager.getActiveNetworkInfo().isAvailable()
    && con_manager.getActiveNetworkInfo().isConnected()) {
  return true;
} else {
  return false;
}
}
}

主要代码是: -

if (!DetectConnection.checkInternetConnection(this)) {
Toast.makeText(getApplicationContext(), "No Internet!",        Toast.LENGTH_SHORT).show();
} else {      
wv = (WebView) findViewById(R.id.donate_webView1);
c = new CustomWebViewClient();
wv.setWebViewClient(c);
wv.clearCache(true);
wv.clearHistory();
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.loadUrl("http://www.google.com");
}


// Function to load all URLs in same webview
private class CustomWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!DetectConnection.checkInternetConnection(this)) {
  Toast.makeText(getApplicationContext(), "No Internet!",Toast.LENGTH_SHORT).show();
} else {
  view.loadUrl(url);
}     
return true;
}
}

我希望它有效:)