Java 如何检查互联网连接(Wifi/移动数据)是否已启用?
如何检查Android中是否启用了internet连接?例如,如果位置服务被禁用,我们将使用该技术Java 如何检查互联网连接(Wifi/移动数据)是否已启用?,java,android,eclipse,android-alertdialog,Java,Android,Eclipse,Android Alertdialog,如何检查Android中是否启用了internet连接?例如,如果位置服务被禁用,我们将使用该技术 Location myLocation = locationmanager.getLastKnownLocation(provider); if(myLocation == null){ Show the AlertDialog. }else{ do this. } 像这样,如何检查Internet连接是否已启用/禁用。对于wifi: ConnectivityManager mgr
Location myLocation = locationmanager.getLastKnownLocation(provider);
if(myLocation == null){
Show the AlertDialog.
}else{
do this.
}
像这样,如何检查Internet连接是否已启用/禁用。对于wifi:
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
connected = networkInfo != null && networkInfo.isConnected();
流动电话:
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
connected = networkInfo != null && networkInfo.isConnected();
对于wifi:
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
connected = networkInfo != null && networkInfo.isConnected();
流动电话:
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
connected = networkInfo != null && networkInfo.isConnected();
您可以使用以下方法:
ConnectivityManager conectivtyManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected()) {
isConnected = true;
} else {
isConnected= false;
}
希望有帮助 您可以使用以下方法:
ConnectivityManager conectivtyManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected()) {
isConnected = true;
} else {
isConnected= false;
}
希望有帮助 可能这个链接可以帮助你,可能这个链接可以帮助你!非常感谢。这有帮助!非常感谢。