java 检查网络和 Internet 连接 - Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31283443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:22:57  来源:igfitidea点击:

Check Network and Internet Connection - Android

javaandroidandroid-internet

提问by spenf10

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

谢谢

采纳答案by wolfaviators

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. Good luck.

在将这篇文章中接受的答案与您的代码进行比较时,您所做的应该有效。随意比较代码。最安全的做法是在飞行模式下运行一些测试,关闭 WiFi,并从远离 WiFi 的位置进行测试,以确保安全。祝你好运。

Android - Programmatically check internet connection and display dialog if notConnected

Android - 以编程方式检查互联网连接并在未连接时显示对话框

回答by Seshu Vinay

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。

回答by Karvendan

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
}

回答by Nelson Katale

I tried the approach of user:3156908but it kept crashing my app when the internet connection was off. Turns out there was a logical error on his code so i used the connectivityManager.getActiveNetworkInfo() != nullto check for network details. In case the device is connected to the network or it is in the process of connecting to the network use the isConnectedOrConnecting()

我尝试了user:3156908的方法,但是当互联网连接关闭时,它一直使我的应用程序崩溃。结果发现他的代码存在逻辑错误,所以我用它connectivityManager.getActiveNetworkInfo() != null来检查网络详细信息。如果设备已连接到网络或正在连接到网络,请使用isConnectedOrConnecting()

In your AndroidManifest.xmlfile 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 checkInternetConnectionin your fragment classor Activity classand add the following lines of code.

创建一个checkInternetConnection在您的片段类Activity 类中调用的方法并添加以下代码行。

 @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 checkInternetConnectionin your fragment's onViewCreatedmethod or your activity's onCreatemethod.

然后你可以checkInternetConnection在片段的onViewCreated方法或活动的onCreate方法中调用。

NoteI would strongly advise you to test it the code when its connected to wifi, mobile data and airplane mode.

注意我强烈建议您在连接到 wifi、移动数据和飞行模式时测试它的代码。