用于检测android手机网络是否可用。
- private boolean checkNetWorkAvailable(Context context) {
- boolean result=false;
- ConnectivityManager mManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- if (mManager!=null) {
- NetworkInfo mNetworkInfo=mManager.getActiveNetworkInfo();
- if (mNetworkInfo!=null&&mNetworkInfo.isConnected()) {
- result=true;
- }
- }
- return result;
- }
用于检测3G/WiFi是否连接:
- private boolean checkWifiOr3GAvailable(Context context) {
- boolean result=false;
- ConnectivityManager mManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- if (mManager!=null) {
- NetworkInfo mNetworkInfo = mManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);//WIFI
- //NetworkInfo mNetworkInfo=mManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);//3G
- if (mNetworkInfo!=null&&mNetworkInfo.isConnected()) {
- result=true;
- }
- }
- return result;
- }