在加载WebView之前检查是否连接到Internet?

时间:2015-03-27 06:52:01

标签: android

我在android中创建了一个webview应用程序我需要实现这个条件,即如果互联网或wifi可用,则意味着它将继续打开weblink。如果互联网或WIFI不可用,则意味着它将加载到我的HTML页面中,该页面存在于资产中。我们怎么能这样做?

package com.example.webview;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;


public class Dadhboard<Bitmap> extends ActionBarActivity {

    WebView web;
    ProgressDialog dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_dadhboard);


        web = (WebView) findViewById(R.id.webview);
        web.setWebViewClient(new WebViewClient() {

            // This method will be triggered when the Page Started Loading

            public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
                dialog = ProgressDialog.show(Dadhboard.this, null,
                        "Please Wait...Page is Loading...");
                dialog.setCancelable(true);
                super.onPageStarted(view, url, favicon);
            }

            public void onPageFinished(WebView view, String url) 
            {
                            dialog.dismiss();
                            super.onPageFinished(view, url);
            }

                        // This method will be triggered when error page appear

            public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) 
            {
                            dialog.dismiss();
                            // You can redirect to your own page instead getting the default
                            // error page
                            Toast.makeText(Dadhboard.this,
                                    "The Requested Page Does Not Exist", Toast.LENGTH_LONG).show();
                            web.loadUrl("http://www.google.com/");
                            super.onReceivedError(view, errorCode, description, failingUrl);
            }
                    });

                    web.loadUrl("http://www.google.com/");
                    web.getSettings().setLoadWithOverviewMode(true);
                    web.getSettings().setUseWideViewPort(true);
                }

}

2 个答案:

答案 0 :(得分:0)

试试这个

连接检测器

package com.likith.conectiondetector;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class connectiondetector 
{
    private Context _context;

/********************************************************************************/

public connectiondetector(Context context)
{
    this._context = context;
}

/********************************************************************************/

public boolean isConnectingToInternet()
{
    ConnectivityManager connectivity = (ConnectivityManager) 
            _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) 
              for (int i = 0; i < info.length; i++) 
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }
      }
      return false;
}
}

使用

connectiondetector cd= new connectiondetector(getApplicationContext());
isInternetPresent = cd.isConnectingToInternet();

if(isInternetPresent)
{
    //Internet is connected
}
else
{
    // Internet is not connected
}

答案 1 :(得分:0)

在您的活动中创建此方法:

public boolean isConnected(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
            Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null &&
            activeNetwork.isConnected(); // isConnectedOrConnecting()
    return isConnected;
}

public boolean isWifi(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
            Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
    return isWiFi;
}

获取是否已连接的状态:

    // check
    if (isConnected(this)) {
        if (isWifi(this)) {
            // connected to wifi
        }else{
            // connected to mobile network
        }
    }else{
        // no network available
    }
相关问题