• android 网络状态判断【转】


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

    在AndroidManifest.xml中增加:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
  • 相关阅读:
    Building Apache Thrift on CentOS 6.5
    ToStringBuilder 学习
    对List中对象的去重
    MyEclipse启动Tomcat服务器时老是跳到Debug调试上
    JS 实现点击展开菜单
    详解公钥、私钥、数字证书的概念 转载
    eclipse svn 忽略 target目录 等等... 我用的后边的方法 (转载)
    Log4j XML 配置
    JS完成改变新闻字体大中小的显示
    Javascript 简单学习
  • 原文地址:https://www.cnblogs.com/fx2008/p/3259231.html
Copyright © 2020-2023  润新知