Android 判断当前网络 wifi ctwap(cmwap) ctnet(cmnet)-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

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"); 
 
 } 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章