0

I created a webview app in android i need to implement the condition i.e. if the internet or wifi is available means it will proceed to open the weblink. If the internet or WIFI is not available means it will load into my HTML page which was present in the asset. How can we able to do it ?

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);
                }

}
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • You can use Timer for setting a time in order to check for Internet and after that displaying a message or some image. – Usman Ali Mar 20 '18 at 12:05

2 Answers2

0

try this

connection detector

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;
}
}

To use

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

if(isInternetPresent)
{
    //Internet is connected
}
else
{
    // Internet is not connected
}
likith sai
  • 527
  • 1
  • 6
  • 21
0

Create this method inside your activity:

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;
}

To get the status whether connected:

    // check
    if (isConnected(this)) {
        if (isWifi(this)) {
            // connected to wifi
        }else{
            // connected to mobile network
        }
    }else{
        // no network available
    }
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87