【轉】關於NetworkInfo對象的isConnected()與isAvailable()


本文轉自 http://blog.csdn.net/santicom/article/details/6932421

測試類如下:

[java]  view plain copy
 
  1. public class MainActivity extends Activity  
  2. {  
  3.     /** Called when the activity is first created. */  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState)  
  6.     {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.   
  10.         this.registerReceiver(mBroadcastReceiver, new IntentFilter(  
  11.                 ConnectivityManager.CONNECTIVITY_ACTION));  
  12.     }  
  13.   
  14.     private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver()  
  15.     {  
  16.         @Override  
  17.         public void onReceive(Context context, Intent intent)  
  18.         {  
  19.             Bundle bundle = intent.getExtras();  
  20.             NetworkInfo aNetworkInfo = (NetworkInfo) bundle  
  21.                     .get(ConnectivityManager.EXTRA_NETWORK_INFO);  
  22.   
  23.             if (aNetworkInfo.isConnected())  
  24.             {  
  25.                 System.out.println("connecte");  
  26.             } else  
  27.             {  
  28.                 System.out.println("not connect");  
  29.             }  
  30.   
  31.             if (aNetworkInfo.isAvailable())  
  32.             {  
  33.                 System.out.println("available");  
  34.             } else  
  35.             {  
  36.                 System.out.println("not available");  
  37.             }  
  38.             System.out.println("--------------------");  
  39.         }  
  40.     };  
  41. }  


總結有以下5種狀態:

在WLAN設置界面

1,顯示連接已保存,但標題欄沒有,即沒有實質連接上,輸出為:not connect, available

2,顯示連接已保存,標題欄也有已連接上的圖標,            輸出為:connect, available

3,選擇不保存后                                                                        輸出為:not connect, available

4,選擇連接,在正在獲取IP地址時                                        輸出為:not connect, not available

5,連接上后                                                                                輸出為:connect, available


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM