Android 判断当前网络 wifi 3gwap(cmwap) 3gnet(cmnet)

Java代码   [url=javascript:void()][/url]
  1. package lab.sodino.net;   
  2.     
  3.  import java.net.InetAddress;   
  4.     
  5.  import android.app.Activity;   
  6.  import android.content.Context;   
  7.  import android.net.ConnectivityManager;   
  8.  import android.net.NetworkInfo;   
  9.  import android.net.wifi.WifiInfo;   
  10.  import android.net.wifi.WifiManager;   
  11.  import android.os.Bundle;   
  12.  import android.widget.ScrollView;   
  13.  import android.widget.TextView;   
  14.     
  15.  public class NetAct extends Activity {   
  16.     private TextView textView;   
  17.     
  18.     public void onCreate(Bundle savedInstanceState) {   
  19.         super.onCreate(savedInstanceState);   
  20.         textView = new TextView(this);   
  21.         textView.setBackgroundColor(0xffffffff);   
  22.         textView.setTextColor(0xff0000ff);   
  23.         textView.setTextSize(15.0f);   
  24.         textView.setScrollBarStyle(TextView.SCROLLBARS_OUTSIDE_OVERLAY);   
  25.     
  26.         ScrollView scrollView = new ScrollView(this);   
  27.         scrollView.addView(textView);   
  28.         setContentView(scrollView);   
  29.         getLocalHost();   
  30.         getWifiInfo();   
  31.         initNetworkInfo();   
  32.     }   
  33.     
  34.     private void getLocalHost() {   
  35.         try {   
  36.             InetAddress iAdd = InetAddress.getLocalHost();   
  37.             String line = "";   
  38.             String hostName = iAdd.getHostName();   
  39.             if (hostName != null) {   
  40.                 InetAddress[] adds = InetAddress.getAllByName(hostName);   
  41.                 for (int i = 0; i  < adds.length; i++) {   
  42.                     iAdd = adds;   
  43.                     line = "HostName=" + iAdd.getHostName() + "\n";   
  44.                     textView.append(line);   
  45.                     line = "CanonicalHostName=" + iAdd.getCanonicalHostName()   
  46.                             + "\n";   
  47.                     textView.append(line);   
  48.                     line = "HostAddress=" + iAdd.getHostAddress() + "\n";   
  49.                     textView.append(line);   
  50.                     textView.append("\n");   
  51.                 }   
  52.             }   
  53.         } catch (Exception e) {   
  54.             e.printStackTrace();   
  55.     
  56.         }   
  57.     }   
  58.     
  59.     public void getWifiInfo() {   
  60.         WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);   
  61.         WifiInfo info = wifi.getConnectionInfo();   
  62.         textView.append("HiddenSSID=" + info.getHiddenSSID() + "\n");   
  63.         textView.append("IpAddress=" + info.getIpAddress() + "\n");   
  64.         textView.append("LinkSpeed=" + info.getLinkSpeed() + "\n");   
  65.         textView.append("NetworkId=" + info.getNetworkId() + "\n");   
  66.         textView.append("Rssi=" + info.getRssi() + "\n");   
  67.         textView.append("SSID=" + info.getSSID() + "\n");   
  68.         textView.append("MacAddress=" + info.getMacAddress() + "\n");   
  69.     }   
  70.     
  71.     public void initNetworkInfo() {   
  72.         ConnectivityManager mag = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);   
  73.         // 此处输出当前可用网络   
  74.         textView.append("\nActive:\n");   
  75.         NetworkInfo info = mag.getActiveNetworkInfo();   
  76.         textView.append("ExtraInfo=" + info.getExtraInfo() + "\n");   
  77.         textView.append("SubtypeName=" + info.getSubtypeName() + "\n");   
  78.         textView.append("TypeName=" + info.getTypeName() + "\n");   
  79.     
  80.         textView.append("\nWifi:\n");   
  81.         NetworkInfo wifiInfo = mag   
  82.                 .getNetworkInfo(ConnectivityManager.TYPE_WIFI);   
  83.     
  84.         textView.append("ExtraInfo=" + wifiInfo.getExtraInfo() + "\n");   
  85.         textView.append("SubtypeName=" + wifiInfo.getSubtypeName() + "\n");   
  86.         textView.append("TypeName=" + wifiInfo.getTypeName() + "\n");   
  87.         NetworkInfo mobInfo = mag   
  88.                 .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);   
  89.         textView.append("\nMobile:\n");   
  90.         textView.append("ExtraInfo=" + mobInfo.getExtraInfo() + "\n");   
  91.         textView.append("SubtypeName=" + mobInfo.getSubtypeName() + "\n");   
  92.         textView.append("TypeName=" + mobInfo.getTypeName() + "\n");   
  93.     }   
  94.  }  

你可能感兴趣的:(Android 判断当前网络 wifi 3gwap(cmwap) 3gnet(cmnet))