简体   繁体   English

如何检查android中连接的真实互联网?

[英]how to check the real Internet connected in android?

I make a program, and I have to use wifi to connect Internet. 我制作一个程序,我必须使用wifi连接互联网。 I find some information to check whether the wifi is connected or not. 我找到一些信息来检查wifi是否已连接。 But in some situation, you can connect the wifi AP but you still can't use Internet like the wifi needed account and password to certificate in https, or the wifi AP is not able to Internet. 但在某些情况下,你可以连接wifi AP,但你仍然无法使用互联网,如WiFi所需的帐户和密码到https的证书,或无线AP无法上网。 So, how can I check the real Internet connected? 那么,我该如何检查真正的互联网连接?

Just do a "Ping" to www.google.com chances that they are down are very low. 只需对www.google.com进行“Ping”,他们失败的可能性非常低。

PS it's what we do in our app.. PS这是我们在我们的应用程序中做的..

public static boolean isReachable(Context context) {
    //  First, check we have connectivity
    final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = connMgr.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnected()) {
        //  check if google is reachable
        try {
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(10 * 1000); // Ten seconds timeout in milliseconds
            urlc.connect();
            if (urlc.getResponseCode() == 200) { // success
                return true;
            } else { // Fail
                return false;
            }
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
            return false;
        }
    } else {
        return false;
    }
}

Please Refer below : 请参阅以下内容:

Make a method that return boolean value : 创建一个返回布尔值的方法:

    ConnectivityManager connectivityManager;
    NetworkInfo wifiInfo, mobileInfo;

    public Boolean checkNow(Context con){
    try{
        connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  

        if(wifiInfo.isConnected() || mobileInfo.isConnected())
        {
            return true;
        }
    }
    catch(Exception e){
        System.out.println("CheckConnectivity Exception: " + e.getMessage());
    }

    return false;
}   

Use the above method in your onCreate() Method Like Below : onCreate()方法中使用上面的方法,如下所示:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       boolean con = checkNow(getApplicationContext());

       if(con){
           Toast.makeText(getApplicationContext(), "Connection Founded", Toast.LENGTH_SHORT).show();
       }else{
           Toast.makeText(getApplicationContext(), "Connection Not Founded", Toast.LENGTH_SHORT).show();
       }

}

When you run the app you will prompted as "Connection Founded" if internet connection available in your device otherwise it will prompted "Connection Not Founded" . 当您运行的应用程序,你会提示为"Connection Founded"如果您的设备提供网络连接,否则会提示"Connection Not Founded"

According to the below method, if device is not in airplane mode or no network available in any connectivity mode, method will return false, otherwise true. 根据以下方法,如果设备未处于飞行模式或在任​​何连接模式下没有网络可用,则方法将返回false,否则为true。 Bare in mind that if above scenarios are satisfied it will return null. 请记住,如果满足以上场景,它将返回null。 Therefor it is handled by checking for null netInfo object. 因此,它通过检查null netInfo对象来处理。

public boolean isOnline() {
ConnectivityManager conManager =
    (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conManager .getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
    return true;
}
return false;

} }

Remember to get permission to access network state to the manifest. 请记住获得访问清单的网络状态的权限。

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

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

相关问题 如何在Android中检查移动网络是否连接到互联网 - How to check if mobile network is connected to the internet or not in android 如何检查Android手机未连接到互联网? - How to check that android phone is not connected to internet? android如何检查wifi已连接但没有互联网连接 - android how to check wifi is connected but no internet connection 如何检查Wifi是否已连接,但Android中无法访问Internet - How to check Wifi is connected, but no Internet access in Android 如何检查我的测试android设备是否已连接到互联网? - How to check My test android device is connected to internet or not? Android-如何检查其他用户是否已连接到互联网? - Android - How to check whether other user is connected to internet? 检查设备是否确实已连接到互联网android - check if device is really connected to the internet android 使用Python脚本检查android设备是否已连接到互联网 - Python script to check if android device is connected to internet 如何显示未在Android中连接的互联网 - How to show internet not connected in android 如何在Android程序中检查wifi路由器是否访问互联网? 手机已连接到wifi路由器,但路由器无法访问互联网? - How check wifi router accessing internet or not In Android Program? Mobile is connected to wifi router but router not accessing internet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM