0

My application is based on webview. I added layout with retry button for no internet activity. If wifi or mobile data is off then application shows layout with retry button. But if wifi or mobile data is connected without active internet connection then it does not shows the layout instead of that it shows the error page. What should i do to detect active internet connection and to overcome this problem?

My Code:

public void internetcheck() {
    ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo mobiledata = connectivityManager.getNetworkInfo(connectivityManager.TYPE_MOBILE);
    NetworkInfo wifi = connectivityManager.getNetworkInfo(connectivityManager.TYPE_WIFI);

    
        if (mobiledata.isConnected()) {

            myWebView.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.GONE);
            mAdView.setVisibility(View.VISIBLE);
            myWebView.reload();


        } else if (wifi.isConnected()) {

            myWebView.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.GONE);
            mAdView.setVisibility(View.VISIBLE);
            myWebView.reload();

        } else {

            myWebView.setVisibility(View.GONE);
            relativeLayout.setVisibility(View.VISIBLE);
            mAdView.setVisibility(View.GONE);


        }


    }

3 Answers3

0

Add this method to check if there is working internet connection -

public boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com"); 
        //You can replace it with your name
            return !ipAddr.equals("");

        } catch (Exception e) {
            return false;
    }
}

Edit

Add above method inside Activity/Fragment as follows -

In Activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       // you other code here
    }

    private boolean isInternetAvailable() {
    // past above methode body here
    }
}

and your internetcheck() method will be as follows -

public void internetcheck() {
    ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo mobiledata = connectivityManager.getNetworkInfo(connectivityManager.TYPE_MOBILE);
    NetworkInfo wifi = connectivityManager.getNetworkInfo(connectivityManager.TYPE_WIFI);

    
        if (mobiledata.isConnected() || wifi.isConnected()) {
            if(isInternetAvailable()){
                myWebView.setVisibility(View.VISIBLE);
                relativeLayout.setVisibility(View.GONE);
                mAdView.setVisibility(View.VISIBLE);
                myWebView.reload();
            }
            //you can put another else in case there is no active internet
        } 
        else {
            myWebView.setVisibility(View.GONE);
            relativeLayout.setVisibility(View.VISIBLE);
            mAdView.setVisibility(View.GONE);
        }             
}

Happy Coding !

Nikhil Sharma
  • 897
  • 1
  • 4
  • 18
0

Maybe duplicate of this thread.

Seems there is no suitable way for checking on UI thread right now for api<23.

Ali
  • 87
  • 8
0

Use the Utils class isInternetAvailable() function to get the Internet status of the device. It automatically supports the deprecated isInternetAvailableLegacy() usage for Android versions less than Android M.

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import androidx.annotation.RequiresApi
class Utils constructor(context: Context,
                        private val logger: Logger) {

    enum class NETWORK_TYPE {
        WIFI,
        CELLULAR,
        ETHERNET
    }

    private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

    @SuppressLint("MissingPermission")
    fun isInternetAvailable(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val transportType = getTransportType()
            if (transportType != null) true
            else isInternetAvailableLegacy()
        } else isInternetAvailableLegacy()
    }

    @Suppress("DEPRECATION")
    @SuppressLint("MissingPermission")
    private fun isInternetAvailableLegacy(): Boolean {
        try {
            val activeNetworkInfo = connectivityManager.activeNetworkInfo
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected) {
                return true
            }
        } catch (e: Exception) {
            // TODO Handle Exception
        }
        return false
    }

    @RequiresApi(Build.VERSION_CODES.M)
    @SuppressLint("MissingPermission")
    private fun getTransportType(): NETWORK_TYPE? {
        val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
        capabilities?.let {
            return when {
                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> NETWORK_TYPE.CELLULAR
                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> NETWORK_TYPE.WIFI
                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> NETWORK_TYPE.ETHERNET
                else -> null
            }
        }
        return null
    }
}
Navjot
  • 1,202
  • 1
  • 11
  • 24