Android 網絡連接–Wifi/3G

獲取網絡連接狀態

隨著3G和Wifi的推廣,越來越多的Android應用程序需要調用網絡資源,檢測網絡連接狀態也就成為網絡應用程序所必備的功能。

Android平臺提供瞭ConnectivityManager  類,用於網絡連接狀態的檢測。

Android開發文檔這樣描述ConnectivityManager 的作用:

 

Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling

Context.getSystemService(Context.CONNECTIVITY_SERVICE).

The primary responsibilities of this class are to:

Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
Send broadcast intents when network connectivity changes
Attempt to "fail over" to another network when connectivity to a network is lost
Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks
下面這個簡單的例子 checkNetworkInfo() 說明瞭如何編程獲取Android手機的當前網絡狀態
[java] private void checkNetworkInfo() 
   { 
       ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
 
       //mobile 3G Data Network  
       State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); 
       txt3G.setText(mobile.toString()); //顯示3G網絡連接狀態  
       //wifi  
       State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); 
       txtWifi.setText(wifi.toString()); //顯示wifi連接狀態  
   } 

 private void checkNetworkInfo()
    {
        ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        //mobile 3G Data Network
        State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        txt3G.setText(mobile.toString()); //顯示3G網絡連接狀態
        //wifi
        State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        txtWifi.setText(wifi.toString()); //顯示wifi連接狀態
    }

註:

根據Android的安全機制,在使用ConnectivityManager時,必須在AndroidManifest.xml中添加<uses-permission
 android:name="android.permission.ACCESS_NETWORK_STATE" /> 否則無法獲得系統的許可。

運行結果(關閉3G及wifi網絡連接的狀態下)

 

調用Android手機的網絡配置界面

使用過Android手機上的手機QQ的朋友,應該知道,當QQ啟動時,如果沒有有效的網絡連接,QQ會提示轉入手機的網絡配置界面。這是如何實現的呢。

[java]  private void checkNetworkInfo() 
    { 
        ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
 
        //mobile 3G Data Network  
        State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); 
        txt3G.setText(mobile.toString()); 
        //wifi  
        State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); 
        txtWifi.setText(wifi.toString()); 
         
        //如果3G網絡和wifi網絡都未連接,且不是處於正在連接狀態 則進入Network Setting界面 由用戶配置網絡連接  
        if(mobile==State.CONNECTED||mobile==State.CONNECTING) 
            return; 
        if(wifi==State.CONNECTED||wifi==State.CONNECTING) 
            return; 
         
         
        startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//進入無線網絡配置界面  
        //startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //進入手機中的wifi網絡設置界面  
         
    } 

private void checkNetworkInfo()
    {
        ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        //mobile 3G Data Network
        State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        txt3G.setText(mobile.toString());
        //wifi
        State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        txtWifi.setText(wifi.toString());
       
        //如果3G網絡和wifi網絡都未連接,且不是處於正在連接狀態 則進入Network Setting界面 由用戶配置網絡連接
        if(mobile==State.CONNECTED||mobile==State.CONNECTING)
            return;
        if(wifi==State.CONNECTED||wifi==State.CONNECTING)
            return;
       
       
        startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//進入無線網絡配置界面
        //startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //進入手機中的wifi網絡設置界面
       
    }
運行結果(關閉3G及wifi網絡連接的狀態下),程序轉入無線網絡配置界面

[java] <SPAN style="FONT-SIZE: 18px"><STRONG>startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//進入無線網絡配置界面</STRONG></SPAN> 

startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//進入無線網絡配置界面

 

如果調用

[java]  <SPAN style="FONT-SIZE: 18px"><STRONG>startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //直接進入手機中的wifi網絡設置界面</STRONG></SPAN> 

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //直接進入手機中的wifi網絡設置界面
則直接進入手機中的wifi網絡設置界面

在wifi網絡連接後 運行該程序

 

我們可以看到 wi-fi 狀態為已連接(CONNECTED).

 

You May Also Like