在Android 中是否已連網可以通過 ConnectivityManager.getActiveNetworkInfo().isAvailable() 來判斷,如果為True則表示當前Android手機已經連網,可能是WiFi或GPRS、HSDPA等等.
需具有權限public String internetState() { String state = "Net:"; ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info=connManager.getActiveNetworkInfo();//wifi if (info == null) state = state + " No WiFi"; if (info.isConnected()) { state = state + " WiFi Online"; if (info.isAvailable()) { state = state + " Available"; } } else state = state + " WiFi Offline"; NetworkInfo mobNetInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE );//3G if (info == null) state = state + " No 3G"; if (info.isConnected()) { state = state + " 3G Online"; if (info.isAvailable()) { state = state + " Available"; } } else state = state + " 3G Offline"; // return state; }
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>