简体   繁体   English

如何检查互联网连接(Wifi /移动数据)是否已启用?

[英]How to check if the Internet connection (Wifi/Mobile data) is ENABLED?

How to check if the internet connection is Enabled in Android? 如何检查互联网连接在Android中是否已启用? Like if the LOCATION SERVICE is disabled we use the technique 就像如果LOCATION SERVICE被禁用,我们使用该技术

Location myLocation = locationmanager.getLastKnownLocation(provider);
if(myLocation == null){
      Show the AlertDialog.
}else{
do this.
}

Like this, how do i check if the Internet connection is Enabled/Disabled. 这样,如何检查Internet连接是否已启用。

For wifi: 对于wifi:

ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
connected = networkInfo != null && networkInfo.isConnected();

For Mobile: 对于手机:

ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
connected = networkInfo != null && networkInfo.isConnected();

You can use the following method: 您可以使用以下方法:

ConnectivityManager conectivtyManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);  
if (conectivtyManager.getActiveNetworkInfo() != null  
    && conectivtyManager.getActiveNetworkInfo().isAvailable()  
    && conectivtyManager.getActiveNetworkInfo().isConnected()) {  
    isConnected = true;  
} else {  
    isConnected= false;  
}  

Hope it helps! 希望能帮助到你!

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

相关问题 如果没有互联网连接/wifi/移动数据(JAVA_CODE),如何将第二个活动支持到第一个活动 - How to back second activity to first activity if no internet connection/wifi/mobile data (JAVA_CODE) 如何检查手机上是否启用了WiFi - How To Check if WiFi Enabled On Phone 如何,当启用wifi和移动网络时,您在android上选择通过移动网络传输数据吗? - How, when wifi and mobile networks are enabled, do you choose to transfer data through the mobile network on android? 检查是否启用了wifi和sharedpreferences - Check if wifi is enabled and sharedpreferences 如何检查:局域网互联网连接? - How to check: LAN internet connection? 如何在Android中检测用户何时连接到wifi或移动互联网 - How to detect when user connects to wifi or mobile internet in android 带WiFi和拨号上网的RMI连接 - RMI connection with wifi and dialup internet 如何通知互联网连接断开,但WiFi仍然连接 - How to be notified of internet connection down, but WiFi still connected Android:如何以编程方式启用/禁用 Wifi 或 Internet 连接 - Android: How to Enable/Disable Wifi or Internet Connection Programmatically Javamail使用wifi连接发送电子邮件,但不支持数据移动 - Javamail Send email work with wifi connection but not with data mobile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM