Android检查是否连接网络

检查是否连接网络 方法:

  //判断是否联网
    private boolean checkConnectNetwork(Context context) {
 
        ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo net = conn.getActiveNetworkInfo();
        if (net != null && net.isConnected()) {
            return true;
        }
        return false;
    }


调用是该方法:

if (!checkConnectNetwork(context)) {
                        new Handler(context.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(context, "请检查连接网络", Toast.LENGTH_LONG).show();
                            }
                        });
                        return;
                    }


————————————————

原文链接:https://blog.csdn.net/baidu_38995168/article/details/107413163

猜你喜欢

转载自blog.csdn.net/weixin_42602900/article/details/126011129