android获取局域网在线ip

最近做网络这块,需要获取wifi 局域网内在线。 以下为查资料所得

写的有点乱  , 有需要的朋友慢慢看吧

以通过测试:

   

public class WifiDemoActivity extends Activity {

private Button mTestButton;

private WifiManager mWifiManager;

private TextView iptextview;

staticintthreadCount = 0;

booleanisOver = false;

staticintpingSize = 0;

public ArrayList<Map<String, Object>> ping; // ping 后的结果集

private ListView showiplistview;

 

public ArrayList<Map<String, Object>> getPing() { // 用来得到ping后的结果集

returnping;

}

   public void setPing(ArrayList<Map<String, Object>> ping){

  this.ping=ping;

   }

@Override

protected void onPause() {

System.exit(0);

finish();

super.onPause();

}

 

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ping = new ArrayList<Map<String, Object>>();

iptextview = (TextView) findViewById(R.id.IPTEXTVIEWID);

mTestButton = (Button) findViewById(R.id.button1);

showiplistview = (ListView) findViewById(R.id.SHOWIPLISTVIEWID);

mTestButton.setOnClickListener(mAllClickListener);

mWifiManager = (WifiManager) this

.getSystemService(Context.WIFI_SERVICE);

}

@Override

protected void onResume() {

isOver = true;

mUpdaHandler.sendMessage(createMessage(10003, null));

super.onResume();

}

private View.OnClickListener mAllClickListener = new View.OnClickListener() {

 

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.button1:

if (ping.size() > 0) {

ping.clear();

((IpAdapter) showiplistview.getAdapter()).setIpData(ping);

}

new Thread() {

public void run() {

WifiInfo wifiInfo = mWifiManager.getConnectionInfo();

mWifiManager.getConfiguredNetworks();

List<WifiConfiguration> list = mWifiManager

.getConfiguredNetworks();

for (WifiConfiguration wifiConfiguration : list) {

String ssid = wifiConfiguration.SSID;

String mSsid = ssid.substring(1, ssid.length() - 1);

String result = wifiConfiguration.toString();

if (mSsid.equalsIgnoreCase(wifiInfo.getSSID())) {

Log.d("Tag", wifiConfiguration.toString());

try {

int i = result.indexOf("LinkAddresses");

int i1 = result.indexOf("Routes");

String ipAddress = result

.substring(i, i1)

.trim()

.substring(

16,

result.substring(i, i1)

.length() - 6);

Log.d("Tag", ipAddress);

int i2 = result.indexOf("DnsAddresses");

String mWifiIntAddress = result

.substring(i2)

.trim()

.substring(

15,

result.substring(i2)

.length() - 4);

Log.d("Tag", "WifiInetAddresses   "

+ mWifiIntAddress);

String[] s = { mWifiIntAddress, ipAddress };

mUpdaHandler.sendMessage(createMessage(

10002, s));

PingAll(mWifiIntAddress);

} catch (Exception e) {

Log.e("Tag", "erro" + e);

}

}

}

}

}.start();

 

break;

 

default:

break;

}

}

};

 

public void PingAll(String hostAddress) {

// 首先得到本机的IP,得到网段

try {

int k = 0;

k = hostAddress.lastIndexOf(".");

String ss = hostAddress.substring(0, k + 1);

for (int i = 1; i <= 250; i++) { // 对所有局域网Ip

String iip = ss + i;

creatLog(WifiDemoActivity.class, "PingAll", iip, null);

Ping(iip);

}

 

// 等着所有Ping结束

while (isOver) {

Thread.sleep(3000);

if (threadCount == 0) {

isOver = false;

int k1 = 0;

String result="";

result=(String)ping.get(0).get("ip");

k1=result.lastIndexOf(".");

result=result.substring(k1+1,result.length());

int mResult=Integer.valueOf(result);

for(Map<String, Object> map:ping){

String s=(String) map.get("ip");

String s1= s.substring(k1+1,s.length());

int mS1=Integer.valueOf(s1);

mResult=mResult>mS1?mResult:mS1;

}

Log.d("Tag", ""+mResult);

mUpdaHandler.sendMessage(createMessage(10003, null));

}

creatLog(WifiDemoActivity.class, "pingall", "wait......", null);

}

} catch (Exception e) {

creatLog(WifiDemoActivity.class, "pingAll", "", e);

}

}

 

public void Ping(String ip) {

// 最多30个线程

try {

while (threadCount > 30) {

Thread.sleep(2);

}

creatLog(WifiDemoActivity.class, "ping", threadCount + "", null);

threadCount += 1;

runPingIPprocess(ip);

} catch (Exception e) {

creatLog(WifiDemoActivity.class, "ping", null, e);

}

}

public void runPingIPprocess(final String ipString) {

final Thread pingThread = new Thread() {

 

@Override

public void run() {

try {

//ping ip地址  

Process p = Runtime.getRuntime().exec(

"ping -c 1 -w 5 " + ipString);

int status = p.waitFor();

if (status == 0) {

Map<String, Object> map = new HashMap<String, Object>();

creatLog(WifiDemoActivity.class, "runpingIpProcesses",

"true" + ipString, null);

map.put("ip", ipString);

ping.add(map);

pingSize++;

if (pingSize > 10) {

pingSize = 0;

//setPing(ping);

mUpdaHandler

.sendMessage(createMessage(10001, ping));

Log.d("Tag", "you are number");

}

creatLog(WifiDemoActivity.class, "run",

ping.toString(), null);

} else {

creatLog(WifiDemoActivity.class, "runpingIPprocess",

"false", null);

}

threadCount--;

creatLog(WifiDemoActivity.class, "run now ",

threadCount + "", null);

} catch (Exception e) {

threadCount--;

creatLog(WifiDemoActivity.class, "runPingIpprocess", null,

e);

} finally {

}

}

};

pingThread.start();

}

private Handler mUpdaHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

IpAdapter adapter ;

switch (msg.what) {

case 10001:

try {

Log.d("Tag", ping.toString() + "over");

ArrayList<Map<String, Object>> pings = (ArrayList<Map<String, Object>>) msg.obj;

adapter = (IpAdapter) showiplistview.getAdapter();

adapter.setIpData(getPing());

creatLog(WifiDemoActivity.class, "10001", pings + "", null);

adapter.notifyDataSetChanged();

                    isOver=true;

} catch (Exception e) {

creatLog(WifiDemoActivity.class, "1001", null, e);

}

break;

case 10002:

String[] s = (String[]) msg.obj;

iptextview.setText("本机IP:" + s[1] + "\n路由器IP:" + s[0]);

break;

case 10003:

adapter = new IpAdapter(WifiDemoActivity.this, ping);

showiplistview.setAdapter(adapter);

break;

default:

break;

}

}

};

public static Message createMessage(int what, Object object) {

Message message = new Message();

message.what = what;

message.obj = object;

return message;

}

public static void creatLog(Class class1, String where, String data,

Exception e) {

if (e != null) {

Log.e(class1.getSimpleName() + "  "+where, "erro \n" + e);

} else if (e == null) {

Log.d(class1.getSimpleName() + "  "+where + "", data + "");

}

}

 

如果哪位朋友有更好点方法,求分享哈撒

转载于:https://www.cnblogs.com/sinde/archive/2012/06/12/wifiOnlineIP.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值