检查,如果我在网络产品,但没有互联网,安卓 [英] Checking if I'm in Network Local but without Internet, Android

查看:131
本文介绍了检查,如果我在网络产品,但没有互联网,安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能只知道,如果我在连接本地网络,但没有连接到互联网,或不接收数据?我想要得到,可以检查它的方法...帮助我,请我在Android的工作

解决方案

这是你如何能做到这一点。

检查网络可用,这样

功能1

 私人布尔isNetworkAvailable(){
    ConnectivityManager厘米=
        (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    的NetworkInfo的NetInfo = cm.getActiveNetworkInfo();
    返回的NetInfo = NULL和放大器;!&安培; netInfo.isConnectedOrConnecting();
}
 

在此,请像这样,如果互联网可

功能2

 公共静态布尔isInternetAccessible(上下文的背景下){
     如果(isWifiAvailable()){
         尝试 {
             HttpURLConnection的urlc =(HttpURLConnection类)(新的URL(http://www.google.com).openConnection());
            urlc.setRequestProperty(用户代理,测试);
            urlc.setRequestProperty(连接,关闭);
            urlc.setConnectTimeout(1500);
            urlc.connect();
            返程(urlc.getResponse code()== 200);
        }赶上(IOException异常E){
            Log.e(LOG_TAG,无法检查网络连接,E);
        }
       } 其他 {
           Log.d(LOG_TAG,网络不可用!);
       }
   返回false;
}
 

  1. 如果功能1返回错误 - >未连接到网络
  2. 如果功能1返回真,功能2返回false - >连接,但没有看房
  3. 如果这两个函数返回true - >互联网访问

注意您将需要这两个权限清单

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
 

How Can I only Know if I'm connected in Network Local but without connection to Internet , or without receive data? I'm trying get a method that can check it ... Help me please I'm working in Android

解决方案

This is how you can do it

Check if network is available like this

function 1

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

After this, check like this if internet is available

function 2

 public static boolean isInternetAccessible(Context context) {
     if (isWifiAvailable()) {
         try {
             HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
            urlc.setRequestProperty("User-Agent", "Test");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(1500); 
            urlc.connect();
            return (urlc.getResponseCode() == 200);
        } catch (IOException e) {
            Log.e(LOG_TAG, "Couldn't check internet connection", e);
        }
       } else {
           Log.d(LOG_TAG, "Internet not available!");
       }
   return false;
}

  1. If function 1 returns false --> Not connected to network
  2. If function 1 return true and function 2 returns false --> Connected but no intenet
  3. If both functions return true --> Internet is accessible

Note You will need these two permissions in the manifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

这篇关于检查,如果我在网络产品,但没有互联网,安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆