如何檢查互聯網連接是否在Android中啓用?就像如果位置服務被禁用,我們使用的技術如何檢查Internet連接(Wifi /移動數據)是否已啓用?
Location myLocation = locationmanager.getLastKnownLocation(provider);
if(myLocation == null){
Show the AlertDialog.
}else{
do this.
}
這樣,我如何檢查Internet連接啓用/禁用。
如何檢查互聯網連接是否在Android中啓用?就像如果位置服務被禁用,我們使用的技術如何檢查Internet連接(Wifi /移動數據)是否已啓用?
Location myLocation = locationmanager.getLastKnownLocation(provider);
if(myLocation == null){
Show the AlertDialog.
}else{
do this.
}
這樣,我如何檢查Internet連接啓用/禁用。
您可以使用下面的方法:
ConnectivityManager conectivtyManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected()) {
isConnected = true;
} else {
isConnected= false;
}
希望它能幫助!
支持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();
它幫助!謝謝! –
可能是這個鏈接可以幫助ü http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available –