首页 > 代码库 > 18_手机网络连接状态

18_手机网络连接状态

  判断手机的网络连接状态,能够识别mobile和wifi连接。

  以下程序,参考自 http://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html

 

  获取网络信息需要在AndroidManifest.xml文件中加入相应的权限。

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 

  1 /**  2  * 判断网络连接状态  3  * 1. mobile网络连接  4  * 2. wifi网络连接  5  * 3. 没有网络连接  6  *   7  * 判断的方法就是:利用ConnectivityManager来获取NetworkInfo。  8  *   9  * 在程序的后面还给了几个函数 10  * 以下程序,参考自 http://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html 11  */ 12  13 package com.example.networkstate; 14  15 import android.app.Activity; 16 import android.content.BroadcastReceiver; 17 import android.content.Context; 18 import android.content.Intent; 19 import android.content.IntentFilter; 20 import android.net.ConnectivityManager; 21 import android.net.NetworkInfo; 22 import android.os.Bundle; 23 import android.widget.TextView; 24  25 public class MainActivity extends Activity { 26  27     private BroadcastReceiver connectionReceiver; 28     private TextView tvNetworkState; 29  30     @Override 31     protected void onCreate(Bundle savedInstanceState) { 32         super.onCreate(savedInstanceState); 33         setContentView(R.layout.activity_main); 34  35         tvNetworkState = (TextView) findViewById(R.id.tvNetworkState); 36  37         connectionReceiver = new BroadcastReceiver() { 38  39             @Override 40             public void onReceive(Context context, Intent intent) { 41                 ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 42                 NetworkInfo mobNetInfo = connectMgr 43                         .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 44                 NetworkInfo wifiNetInfo = connectMgr 45                         .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 46  47                 if (mobNetInfo.isConnected()) { 48                     tvNetworkState.setText("手机网络连接~"); 49                 } else if (wifiNetInfo.isConnected()) { 50                     tvNetworkState.setText("wifi网络连接~"); 51                 } else { 52                     tvNetworkState.setText("囧,没有网络连接~"); 53                 } 54  55                 // if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) 56                 // { 57                 // // unconnect network 58                 // tvNetworkState.setText("当前没有网络连接!"); 59                 // } else { 60                 // // connect network 61                 // tvNetworkState.setText("恭喜,有网络连接~"); 62                 // } 63             } 64  65         }; 66  67         IntentFilter intentFilter = new IntentFilter(); 68         intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 69         registerReceiver(connectionReceiver, intentFilter); 70     } 71  72     @Override 73     protected void onDestroy() { 74         super.onDestroy(); 75         if (connectionReceiver != null) { 76             unregisterReceiver(connectionReceiver); 77         } 78     } 79  80     public boolean isNetworkConneted(Context context) { 81         if (context != null) { 82             ConnectivityManager connectivityManager = (ConnectivityManager) context 83                     .getSystemService(Context.CONNECTIVITY_SERVICE); 84             NetworkInfo networkInfo = connectivityManager 85                     .getActiveNetworkInfo(); 86             if (networkInfo != null) { 87                 return networkInfo.isAvailable(); 88             } 89         } 90         return false; 91     } 92  93     public boolean isWifiConnected(Context context) { 94         if (context != null) { 95             ConnectivityManager connectivityManager = (ConnectivityManager) context 96                     .getSystemService(Context.CONNECTIVITY_SERVICE); 97             NetworkInfo networkInfo = connectivityManager 98                     .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 99             if (networkInfo != null) {100                 return networkInfo.isAvailable();101             }102         }103         return false;104     }105 106     public boolean isMobileConnected(Context context) {107         if (context != null) {108             ConnectivityManager mConnectivityManager = (ConnectivityManager) context109                     .getSystemService(Context.CONNECTIVITY_SERVICE);110             NetworkInfo mMobileNetworkInfo = mConnectivityManager111                     .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);112             if (mMobileNetworkInfo != null) {113                 return mMobileNetworkInfo.isAvailable();114             }115         }116         return false;117     }118 119     public static int getConnectedType(Context context) {120         if (context != null) {121             ConnectivityManager mConnectivityManager = (ConnectivityManager) context122                     .getSystemService(Context.CONNECTIVITY_SERVICE);123             NetworkInfo mNetworkInfo = mConnectivityManager124                     .getActiveNetworkInfo();125             if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {126                 return mNetworkInfo.getType();127             }128         }129         return -1;130     }131 }

 

18_手机网络连接状态