方法如下: 
ConnectivityManager cManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cwjManager.getActiveNetworkInfo(); 
  if (info != null && info.isAvailable()){ 
       //do something 
       //能上網 
        return true; 
  }else{ 
       //do something 
       //不能上網
        return false; 
  } 
如果為True則表示目前Android手機已經連上網路,可能是WiFi或GPRS、HSDPA等等,具體的可以通過 ConnectivityManager 類的getActiveNetworkInfo() 方法判斷詳細的接入方式。 

同時要在manifest裡面加個權限 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

內容如下: 
boolean android.net.NetworkInfo.isAvailable() 

public boolean isAvailable () 
Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network. Examples include 

The device is out of the coverage area for any network of this type. 
The device is on a network other than the home network (i.e., roaming), and data roaming has been disabled. 
The device's radio is turned off, e.g., because airplane mode is enabled. 

Returns 
true if the network is available, false otherwise

=======================================================================

WifiManager wifi_service = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiinfo = wifi_service.getConnectionInfo();
Log.v("getBSSID", wifiinfo.getBSSID() + "");
Log.v("getHiddenSSID", wifiinfo.getBSSID() + "");
Log.v("getIpAddress", wifiinfo.getIpAddress() + "");
Log.v("getLinkSpeed", wifiinfo.getLinkSpeed() + "");
Log.v("getMacAddress", wifiinfo.getMacAddress() + "");
Log.v("getNetworkId", wifiinfo.getNetworkId() + "");
Log.v("getRssi", wifiinfo.getRssi() + "");
Log.v("getSSID", wifiinfo.getSSID() + "");


DhcpInfo dhcpinfo = wifi_service.getDhcpInfo();
Log.v("ipaddr", dhcpinfo.ipAddress + "");
Log.v("gatewau", dhcpinfo.gateway + "");
Log.v("netmask", dhcpinfo.netmask + "");
Log.v("dns1", dhcpinfo.dns1 + "");
Log.v("dns1", dhcpinfo.dns2 + "");
Log.v("serverAddress", dhcpinfo.serverAddress + "");

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()