代码与范例:
1 2 3 4 |
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI; |
说明:通过 isConnected 与 isWiFi 可以判断设备当前是否建立数据通信连接,及连接类型是否为 WiFi。