I am developing an WiFi app for the purpose of SMS forwarding .After turning on the WiFi button and open the app it will display the Ip address like 10.0.0.3 and port number is 33300 in the corresponding edit boxes.The problem is when the app is open when i switch off the WiFi button it should display empty but still it display the IPAddress .Also again when i switch on the WiFi button it should display the IPAddress .How to check the continuously WiFi Address at the background and display it in the textbox

public class WifiFragment  extends Fragment {
EditText et_portnumber,et_IpAddress;
int port=33300;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
        {

    View rootView = inflater.inflate(R.layout.wififragment, container, false);
    et_portnumber = (EditText) rootView.findViewById(R.id.etPortnumber);
    et_IpAddress= (EditText) rootView.findViewById(R.id.ipaddress);
    btn_serverOn=(Button)rootView.findViewById(R.id.btn_Ok);
    final String strgetIpdaddress= getIpAddress();
    et_IpAddress.setText(strgetIpdaddress);
    et_portnumber.setText(port);
            }
private String getIpAddress() {
    String ip = "";
    try {
        Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
                .getNetworkInterfaces();
        while (enumNetworkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = enumNetworkInterfaces   .nextElement();
            Enumeration<InetAddress> enumInetAddress = networkInterface
                    .getInetAddresses();
            while (enumInetAddress.hasMoreElements()) {
                InetAddress inetAddress = enumInetAddress.nextElement();
                if (inetAddress.isSiteLocalAddress()) {
                    ip =  inetAddress.getHostAddress() + "\n";
                    }
            }
        }
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        ip += "Something Wrong! " + e.toString() + "\n";
        }
    return ip;
}

}

Related posts

Recent Viewed