java - 没有互联网显示对话框

标签 java android android-studio

我有一个应用程序,有两种类型的“无互联网错误”。 当您启动应用程序时,它会查看您是否有互联网并显示错误无互联网,并且我的 WebView 中有一个指向自定义页面的loadURL

我想隐藏webview网站,但是用自定义页面,需要一点时间,就可以看到错误+网站。

可以满足我的需求吗?

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout. fragment_paginainicio, container, false);
        final WebView mWebView = (WebView) view.findViewById(R.id.webView_websitepage);
        mWebView.loadUrl("https://google.pt");
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                mWebView.loadUrl("file:///android_asset/errorpage.html");

            }

        });

最佳答案

You have to check if internet is available. Below code will help in that case. If it is not available you can simple make webview visiblity to gone or do not load anything and show alert dialog for no network 

public static boolean isNetworkAvailable(Context mContext) {

                ConnectivityManager connectivityManager
                        = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                return activeNetworkInfo != null && activeNetworkInfo.isConnected();

        }

关于java - 没有互联网显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58460631/

相关文章:

java - ArrayList 转换为 JList。带有 clientlist\channellist 的 RMI 聊天程序

java - LogCat 垃圾邮件 GC_FOR_MALLOC 但只使用了 50% 的内存

java - 启动应用程序时在 Android 上找不到类异常

java - Android 应用程序在列出 ListView 的项目时崩溃

java - 在 Java 中绘制图元的最简单方法(不是 OpenGL)

java - 蠕虫(蛇)游戏,棋子生长/move

android - Android中哪些方法需要android.permission.DEVICE_POWER?

android - 无法在Android上访问Google map

android - "Task ' 编译 ' is ambiguous in root project"- Android Studio

java - Android Studio 中的冲突依赖项