L
L
lamoshnik2019-01-07 04:41:52
Java
lamoshnik, 2019-01-07 04:41:52

How to implement correct internet connection check under Android webview?

I register an Internet connection check in MainActivity:

private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
    }

In onCreate I insert the class:
if (!isNetworkAvailable()) {

            Toast.makeText(getApplicationContext(), "You have no Internet Connection!", Toast.LENGTH_SHORT).show();
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
            finish();
        }

As a result, instead of closing the application in the absence of an Internet connection, I get an error (The page is not available, even when there is a connection).
What have I done wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question