I am programming android APP.
I have the activity responsible for Pinging 254 IPv4 and placing connected machines in the database table (setting ip) when I click the Auto Scan button. // ------------------------------------------------ ---------------------
First, I get the local IP (for example, 192.168.1.8), then when the button is pressed, I retrieve the subString from ip (192.168.1 for this example), then I start pinging all IP addresses, starting with "192.168. 1.1" and ending with "192.168.1.254". after each ping I do a test to find out if this ping succeeded or failed, if it succeeded, I placed this ip in the database table and then finally I will show a list of connected IP addresses in the list. // ------------------------------------------------ ---------------------
The problem is that the Pinging task takes too long to complete (about 15 minutes or more !!), this is crazy. I tried using InetAddress.isReachable (), but it could not find a computer that works with windows, so I changed it to work with Process and Runtime.getRuntime (). Exec (cmd).
Below is the Android code:
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]+" %"); } }
and the method that pings:
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 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; } }
source share