2014-03-31 46 views
0

如何检查互联网连接是否在Android中启用?就像如果位置服务被禁用,我们使用的技术如何检查Internet连接(Wifi /移动数据)是否已启用?

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

这样,我如何检查Internet连接启用/禁用。

+0

可能是这个链接可以帮助ü http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available –

回答

2

您可以使用下面的方法:

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

希望它能帮助!

2

支持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(); 
+0

它帮助!谢谢! –

相关问题