1
我正在開發一個用於短信轉發的WiFi應用程序。開啓WiFi按鈕並打開應用程序後,它將顯示Ip地址,如10.0.0.3和端口號是33300在相應的編輯框。問題是當應用程序打開時,我關閉的WiFi按鈕它應該顯示空,但它仍然顯示IPAddress.Also當我開啓WiFi按鈕時,它應該顯示IPAddress 。如何檢查背景中的連續WiFi地址並將其顯示在文本框中在android如何在後臺連續檢查WiFi是否啓用
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;
}
}