package com.example.iptest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class IpText<TextHandler> extends ActionBarActivity {
private static final String DEBUG_TAG = null;
public String getLocalIpAddress()
{
final String IP_NONE = "N/A";
final String WIFI_DEVICE_PREFIX = "eth";
String LocalIP = IP_NONE;
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
if( LocalIP.equals(IP_NONE) )
LocalIP = inetAddress.getHostAddress().toString();
else if( intf.getName().startsWith(WIFI_DEVICE_PREFIX) )
LocalIP = inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException e) {
Log.e(DEBUG_TAG, "getLocalIpAddress Exception:"+e.toString());
}
return LocalIP;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ip_text, menu);
setContentView(R.layout.mrchu);
TextView Ipttext = (TextView)findViewById(R.id.Ipttext);
Ipttext.setVisibility(View.VISIBLE);
Ipttext.setText("LocalIP");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
IP를 불러온다는 코드로 하여금 하고 Settext로 하여 IP를 보내서 출력하는 방법이 가능한건가요??
IP를 저기에 넣어야 하는데.. 어디에 뭘추가해야 될까요?
스레드 사용해서 보내야할 것 같은데...;;