如何检查 Android 中是否启用了互联网连接?就像如果位置服务被禁用我们使用该技术
Location myLocation = locationmanager.getLastKnownLocation(provider);
if(myLocation == null){
Show the AlertDialog.
}else{
do this.
}
像这样,我如何检查互联网连接是否启用/禁用。
最佳答案
您可以使用以下方法:
ConnectivityManager conectivtyManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected()) {
isConnected = true;
} else {
isConnected= false;
}
希望对你有帮助!
关于java - 如何检查互联网连接(Wifi/移动数据)是否已启用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22766807/