Android 判断当前网络 wifi ctwap(cmwap) ctnet(cmnet)

 
package lab.sodino.net; 
  
 import java.net.InetAddress; 
  
 import android.app.Activity; 
 import android.content.Context; 
 import android.net.ConnectivityManager; 
 import android.net.NetworkInfo; 
 import android.net.wifi.WifiInfo; 
 import android.net.wifi.WifiManager; 
 import android.os.Bundle; 
 import android.widget.ScrollView; 
 import android.widget.TextView; 
  
 public class NetAct extends Activity { 
  private TextView textView; 
  
  public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  textView = new TextView(this); 
  textView.setBackgroundColor(0xffffffff); 
  textView.setTextColor(0xff0000ff); 
  textView.setTextSize(15.0f); 
  textView.setScrollBarStyle(TextView.SCROLLBARS_OUTSIDE_OVERLAY); 
  
  ScrollView scrollView = new ScrollView(this); 
  scrollView.addView(textView); 
  setContentView(scrollView); 
  getLocalHost(); 
  getWifiInfo(); 
  initNetworkInfo(); 
 
  
  private void getLocalHost() { 
  try { 
  InetAddress iAdd = InetAddress.getLocalHost(); 
  String line = ""; 
  String hostName = iAdd.getHostName(); 
  if (hostName != null) { 
  InetAddress[] adds = InetAddress.getAllByName(hostName); 
  for (int i = 0; i  < adds.length; i++) { 
  iAdd = adds[i]; 
  line = "HostName=" + iAdd.getHostName() + "\n"; 
  textView.append(line); 
  line = "CanonicalHostName=" + iAdd.getCanonicalHostName() 
  + "\n"; 
  textView.append(line); 
  line = "HostAddress=" + iAdd.getHostAddress() + "\n"; 
  textView.append(line); 
  textView.append("\n"); 
 
 
  } catch (Exception e) { 
  e.printStackTrace(); 
  
 
 
  
  public void getWifiInfo() { 
  WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
  WifiInfo info = wifi.getConnectionInfo(); 
  textView.append("HiddenSSID=" + info.getHiddenSSID() + "\n"); 
  textView.append("IpAddress=" + info.getIpAddress() + "\n"); 
  textView.append("LinkSpeed=" + info.getLinkSpeed() + "\n"); 
  textView.append("NetworkId=" + info.getNetworkId() + "\n"); 
  textView.append("Rssi=" + info.getRssi() + "\n"); 
  textView.append("SSID=" + info.getSSID() + "\n"); 
  textView.append("MacAddress=" + info.getMacAddress() + "\n"); 
 
  
  public void initNetworkInfo() { 
  ConnectivityManager mag = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
  // 此处输出当前可用网络 
  textView.append("\nActive:\n"); 
  NetworkInfo info = mag.getActiveNetworkInfo(); 
  textView.append("ExtraInfo=" + info.getExtraInfo() + "\n"); 
  textView.append("SubtypeName=" + info.getSubtypeName() + "\n"); 
  textView.append("TypeName=" + info.getTypeName() + "\n"); 
  
  textView.append("\nWifi:\n"); 
  NetworkInfo wifiInfo = mag 
  .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
  
  textView.append("ExtraInfo=" + wifiInfo.getExtraInfo() + "\n"); 
  textView.append("SubtypeName=" + wifiInfo.getSubtypeName() + "\n"); 
  textView.append("TypeName=" + wifiInfo.getTypeName() + "\n"); 
  NetworkInfo mobInfo = mag 
  .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
  textView.append("\nMobile:\n"); 
  textView.append("ExtraInfo=" + mobInfo.getExtraInfo() + "\n"); 
  textView.append("SubtypeName=" + mobInfo.getSubtypeName() + "\n"); 
  textView.append("TypeName=" + mobInfo.getTypeName() + "\n"); 
 
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值