当前位置:操作系统 > 安卓/Android >>

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

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,