我正在编写一个Android应用程序。
我有一个活动负责Ping 254个IPv4地址,并在单击“自动扫描”按钮时将连接的计算机放入数据库表中(放置IP)。 //---------------------------------------------------------------------
首先,我获取本地IP(例如192.168.1.8),然后当单击按钮时,从IP提取子字符串(对于此示例为192.168.1.),然后开始从“192.168.1.1”开始并以“192.168.1.254”结束Ping所有IP。每次Ping之后,我都进行测试以了解该Ping是否成功或失败,如果成功,则将该IP放入数据库表中,最后我会在列表视图中显示已连接的IP列表。 //---------------------------------------------------------------------
问题是Ping任务需要太长时间才能完成(大约15分钟或更长时间!),这太疯狂了。我尝试使用InetAddress.isReachable(),速度很快,但它无法找到在Windows上运行的计算机,因此我改用Process和Runtime.getRuntime().exec(cmd)。
以下是Android代码:
class MyTask extends AsyncTask<String, Integer, Void> {
Dialog dialog;
ProgressBar progressBar;
TextView tvLoading,tvPer;
Button btnCancel;
@Override
protected Void doInBackground(String... params) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
String s="", address = params[0];
int index=0,j=0,count=0;
//on prend le dernier index du char "."
final StringBuilder ss = new StringBuilder(address);
index = ss.lastIndexOf(".");
//on prend une souschaîne qui contient l'ipv4 sans le dernier nombre
s = address.substring(0,index+1);
//Tester tous les adresses Ipv4 du même plage d'adresses
for(int i=1; i<255; i++){
if (isCancelled()) {
break;
}
if(testping(s+""+i+"")){
insertIPv4(s+""+i+"");}
count++;
if(count == 5 && j<100){
count=0;
j+=2;
publishProgress(j);
}
}
return null;
}
@Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
dialog.dismiss();
AlertDialog alert = new AlertDialog.Builder(Gestion_Machines.this)
.create();
alert.setTitle("Terminé");
alert.setMessage("Operation Terminé avec Succés");
alert.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new Dialog(Gestion_Machines.this);
dialog.setCancelable(false);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progressdialog);
progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar1);
tvLoading = (TextView) dialog.findViewById(R.id.tv1);
tvPer = (TextView) dialog.findViewById(R.id.tvper);
btnCancel = (Button) dialog.findViewById(R.id.btncancel);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
objMyTask.cancel(true);
dialog.dismiss();
}
});
dialog.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
tvLoading.setText("Loading... " + values[0] + " %");
tvPer.setText(values[0]+" %");
}
}
制造ping的方法:
public boolean testping(String x){
int exit=22;
Process p;
try {
//.....edited line.....
p = Runtime.getRuntime().exec("ping -c1 -W1 "+x);
//make shure that is -W not -w it's not he same.
p.waitFor();
exit = p.exitValue();
p.destroy();
} catch (IOException ee) {
ee.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (exit == 0) {
return true;
}else{
return false;
}
}