简体   繁体   English

互联网连接可用时正在加载离线页面

[英]Offline Page is getting loaded when internet connection is available

Trying to build / test an android WebView app with online & offline features.尝试构建/测试具有在线和离线功能的 android WebView 应用程序。

But it always load offline page even when internet connection is available.但即使互联网连接可用,它也总是加载离线页面。

MainActivity.java : 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.尝试构建/测试具有在线和离线功能的 android WebView 应用程序。

But it always load offline page even when internet connection is available.但即使互联网连接可用,它也总是加载离线页面。

Trying to build / test an android WebView app with online & offline features.尝试构建/测试具有在线和离线功能的 android WebView 应用程序。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户离线时如何使用自定义的“无Internet连接可用”布局? - How to use a custom No Internet Connection Available layout when user is offline? 无法连接互联网时应用崩溃 - App crashing when internet connection is not available 互联网离线时如何维护Channel API连接 - How to maintain Channel API connection when internet is offline 没有互联网连接时显示刷新页面按钮 - showing refresh page button when there is no internet connection 没有使用Firebase的互联网连接时如何获取对手用户的在线/离线状态 - How to get online/offline state of the opponent user when there is no internet connection using firebase 当 Android Studio API 30 中没有互联网连接时,将用户带到离线活动屏幕 - Take user to offline activity screen when no internet connection in Android Studio API 30 检查是否可以通过代理使用Internet连接 - Check if Internet connection is available through proxy 如果没有可用的互联网连接,则我的应用中存在ANR - ANR in my app if no internet connection available 仅当互联网连接可用时才允许切换开关 - Allow switch to toggle only if internet connection is available RxJava / RxAndroid检查互联网连接是否可用 - RxJava/RxAndroid Check internet connection available or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM