简体   繁体   中英

Offline Page is getting loaded when internet connection is available

Trying to build / test an android WebView app with online & offline features.

But it always load offline page even when internet connection is available.

MainActivity.java :

package com.trial_test.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import java.net.InetAddress;


public class MainActivity extends Activity {

    private WebView mWebView;

    @Override
    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new MyWebViewClient());

        if (isNetworkConnected()) {

            boolean is_test = isInternetAvailable();
            
            if (isInternetAvailable()) {
                
                // REMOTE RESOURCE
                mWebView.loadUrl("https://example.com");
            } else {
                // LOCAL RESOURCE
                mWebView.loadUrl("file:///android_asset/index.html");
            }
        } else {
            // LOCAL RESOURCE
            mWebView.loadUrl("file:///android_asset/index.html");
        }


        }



    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }


    private boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
    }

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


}

Trying to build / test an android WebView app with online & offline features.

But it always load offline page even when internet connection is available.

Trying to build / test an android WebView app with online & offline features.

But it always load offline page even when internet connection is available.

files

This was not enough to check internet connection, You may try this

public class NetworkState {

    public Boolean isNetworkAvailable(Context context)  {
        if (context == null)
            return false;
        ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            NetworkCapabilities capabilities =
                    connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
            if (capabilities != null) {
                if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                    return true;
                } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    return true;
                } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
                    return true;
                }
            }
        } else {
            try {
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
//                        Log.i("update_statut", "Network is available : true")
                    return true;
                }
            } catch ( Exception e) {

            }
        }
        Log.e("update_statut", "Network is available : FALSE ");
        return false;
    }
}

Then Check your connection

 NetworkState networkState =new NetworkState();
if (networkState.isNetworkAvailable(this)) {
            //You are online
}else{
        //You are offline
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM