[英]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.