android 网络状态判断【转】

时间:2021-08-17 22:42:11
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");
        }
}

在AndroidManifest.xml中增加:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>