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() + " ";
textView.append(line);
line = "CanonicalHostName=" + iAdd.getCanonicalHostName()
+ " ";
textView.append(line);
line = "HostAddress=" + iAdd.getHostAddress() + " ";
textView.append(line);
textView.append(" ");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getWifiInfo() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
textView.append("HiddenSSID=" + info.getHiddenSSID() + " ");
textView.append("IpAddress=" + info.getIpAddress() + " ");
textView.append("LinkSpeed=" + info.getLinkSpeed() + " ");
textView.append("NetworkId=" + info.getNetworkId() + " ");
textView.append("Rssi=" + info.getRssi() + " ");
textView.append("SSID=" + info.getSSID() + " ");
textView.append("MacAddress=" + info.getMacAddress() + " ");
}
public void initNetworkInfo() {
ConnectivityManager mag = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// 此处输出当前可用网络
textView.append(" Active: ");
NetworkInfo info = mag.getActiveNetworkInfo();
textView.append("ExtraInfo=" + info.getExtraInfo() + " ");
textView.append("SubtypeName=" + info.getSubtypeName() + " ");
textView.append("TypeName=" + info.getTypeName() + " ");
textView.append(" Wifi: ");
NetworkInfo wifiInfo = mag
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
textView.append("ExtraInfo=" + wifiInfo.getExtraInfo() + " ");
textView.append("SubtypeName=" + wifiInfo.getSubtypeName() + " ");
textView.append("TypeName=" + wifiInfo.getTypeName() + " ");
NetworkInfo mobInfo = mag
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
textView.append(" Mobile: ");
textView.append("ExtraInfo=" + mobInfo.getExtraInfo() + " ");
textView.append("SubtypeName=" + mobInfo.getSubtypeName() + " ");
textView.append("TypeName=" + mobInfo.getTypeName() + " ");
}
}
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() + " ";
textView.append(line);
line = "CanonicalHostName=" + iAdd.getCanonicalHostName()
+ " ";
textView.append(line);
line = "HostAddress=" + iAdd.getHostAddress() + " ";
textView.append(line);
textView.append(" ");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getWifiInfo() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
textView.append("HiddenSSID=" + info.getHiddenSSID() + " ");
textView.append("IpAddress=" + info.getIpAddress() + " ");
textView.append("LinkSpeed=" + info.getLinkSpeed() + " ");
textView.append("NetworkId=" + info.getNetworkId() + " ");
textView.append("Rssi=" + info.getRssi() + " ");
textView.append("SSID=" + info.getSSID() + " ");
textView.append("MacAddress=" + info.getMacAddress() + " ");
}
public void initNetworkInfo() {
ConnectivityManager mag = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// 此处输出当前可用网络
textView.append(" Active: ");
NetworkInfo info = mag.getActiveNetworkInfo();
textView.append("ExtraInfo=" + info.getExtraInfo() + " ");
textView.append("SubtypeName=" + info.getSubtypeName() + " ");
textView.append("TypeName=" + info.getTypeName() + " ");
textView.append(" Wifi: ");
NetworkInfo wifiInfo = mag
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
textView.append("ExtraInfo=" + wifiInfo.getExtraInfo() + " ");
textView.append("SubtypeName=" + wifiInfo.getSubtypeName() + " ");
textView.append("TypeName=" + wifiInfo.getTypeName() + " ");
NetworkInfo mobInfo = mag
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
textView.append(" Mobile: ");
textView.append("ExtraInfo=" + mobInfo.getExtraInfo() + " ");
textView.append("SubtypeName=" + mobInfo.getSubtypeName() + " ");
textView.append("TypeName=" + mobInfo.getTypeName() + " ");
}
}
在AndroidManifest.xml中增加:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>