简体   繁体   English

检查网络和Internet连接-Android

[英]Check Network and Internet Connection - Android

I was wondering if the method below check that I am both connected to a network, and can actually connected to the internet as well. 我想知道下面的方法是否可以检查我是否都已连接到网络,并且实际上也可以连接到互联网。

Not just connected to a network that won't let me access the internet ? 不只是连接到不允许我访问互联网的网络吗?

public boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    boolean isAvailable = false;
    if (networkInfo != null && networkInfo.isConnected()) {
        isAvailable = true;
    }
    return isAvailable;
}

I think, it does but I am not 100% sure. 我认为确实如此,但我不确定100%。

Thanks 谢谢

On comparing the accepted answer on this post to your code, what you are doing should work. 通过比较这篇文章中可接受的答案与您的代码,您正在做什么。 Feel free to compare code. 随意比较代码。 The safest thing to do would be to run a few tests from airplane mode, with the WiFi turned off, and from a location away from WiFi just to be sure. 最安全的做法是在飞机模式下,关闭WiFi并在远离WiFi的位置进行一些测试,以确保安全。 Good luck. 祝好运。

Android - Programmatically check internet connection and display dialog if notConnected Android-以编程方式检查互联网连接并显示对话框(如果未连接)

Have a look into one of my old answers . 看一下我的旧答案之一 It has two different methods 1. to check if a device is connected to a network 2. to check if a device is connected to Internet. 它有两种不同的方法:1.检查设备是否连接到网络2.检查设备是否连接到Internet。

The code below will check the internet connectivity using kotlin in android studio: 下面的代码将使用android studio中的kotlin检查互联网连接:

private fun amIConnected(): Boolean {
    val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val activeNetworkInfo = connectivityManager.activeNetworkInfo
    return activeNetworkInfo != null && activeNetworkInfo.isConnected
}

I tried the approach of user:3156908 but it kept crashing my app when the internet connection was off. 我尝试使用user:3156908的方法,但是当Internet连接断开时,它不断使我的应用程序崩溃。 Turns out there was a logical error on his code so i used the connectivityManager.getActiveNetworkInfo() != null to check for network details. 事实证明,他的代码存在逻辑错误,因此我使用connectivityManager.getActiveNetworkInfo() != null检查网络详细信息。 In case the device is connected to the network or it is in the process of connecting to the network use the isConnectedOrConnecting() 如果设备已连接到网络或正在连接到网络,请使用isConnectedOrConnecting()

In your AndroidManifest.xml file add the following 在您的AndroidManifest.xml文件中添加以下内容

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

Create a method called checkInternetConnection in your fragment class or Activity class and add the following lines of code. 在您的片段类Activity类中创建一个名为checkInternetConnection的方法,并添加以下代码行。

 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private boolean checkInternetConnection(){
        ConnectivityManager connectivityManager = (ConnectivityManager) mcontext.getSystemService(mcontext.CONNECTIVITY_SERVICE);
        return connectivityManager.getActiveNetworkInfo() != null
                && connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting();
    }

Then you can call the checkInternetConnection in your fragment's onViewCreated method or your activity's onCreate method. 然后,您可以在片段的onViewCreated方法或活动的onCreate方法中调用checkInternetConnection

Note I would strongly advise you to test it the code when its connected to wifi, mobile data and airplane mode. 注意我强烈建议您在将其连接到wifi,移动数据和飞行模式时对其代码进行测试。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM