I want to update onprogressupdate method, but can't figure out how. My 
asynctask calls IpScanCallable class and gets results from it using 
ExecutorSerivce and Future interface. however, I don't know how to update 
progress. The IpScanCallable interface returns an object of type Node, and 
so the onprogressupdate method. but when I call publishprogress(future) it 
won't work. anyway how can I update results? and populate the arraylist 
thanks
 /*
 * AscynTask to scan the network
 * you should try different timeout for your network/devices
 * it will try to detect localhost ip addres and subnet. then
 * it will use subnet to scan network
 */
private class TaskScanNetwork extends AsyncTask<Void, Node, Void> {
 static final int SCAN_THREADS = 8;
 static final int numOfHost = 254;
 String subnet = ip.substring(0, ip.lastIndexOf("."));;
 int startIp = 1;
 int endIp = startIp + SCAN_THREADS;

 static final int timeout = 1000;
 ExecutorService ipScanExecutor = Executors.newFixedThreadPool(SCAN_THREADS
);
 List<Future<Node>> ipList = new ArrayList<>();


 @Override
 protected void onPreExecute() {
 Log.v("Ip Address: ", ip);
 hostList.clear();
 scanProgress.setMax(numOfHost);
 statusMsg.setText("Scanning " + subnet + ".0/24");
 }

 @Override
 protected Void doInBackground(Void... params) {
 for (int i = 0; i <= SCAN_THREADS; i++) {
 //if canceled terminate AsyncTask
 if(isCancelled()) {
 break;
 }
 IpScanCallable ipScanCallable = new IpScanCallable(subnet, startIp, endIp);
 Future<Node> futureResults = ipScanExecutor.submit(ipScanCallable);
 startIp = endIp + 1;
 endIp = startIp + SCAN_THREADS;
 ipList.add(futureResults);
 }
 
 ipScanExecutor.shutdown();

 publishProgress();
 return null;
 }

 @Override
 protected void onProgressUpdate(Node... values) {
 //only add to ArrayList<Node> if host is alive on network
 if(values[0].ip != null) {
 hostList.add(values[0]);
 networkAdapter.notifyDataSetInvalidated();
 Toast.makeText(getApplicationContext(), values[0].getIp(), Toast.
LENGTH_SHORT).show();
 }
 //update progress bar
 scanProgress.setProgress(values[0].progressBar);
 }

 @Override
 protected void onPostExecute(Void aVoid) {
 statusMsg.setText(subnet + ".0/24 " + " Hosts");
 }
}

IpScanCallable class

package com.example.android.droidscanner;

import android.util.Log;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.Callable;

/**
 * Created by jlvaz on 3/28/2017.
 */

public class IpScanCallable implements Callable<Node> {
    private Node newNode;
    private int timeout = 1000;
    private int progress;
    private String ip;
    private String network;
    private int startIp;
    private int endIp;

    public IpScanCallable(String network, int startIp, int endIp) {
        this.network = network;
        this.startIp = startIp;
        this.endIp = endIp;
    }

    @Override
    public Node call() throws Exception {
        for(int i = startIp; i <= endIp; i++) {
            ip = network + "." + i;
            Log.v("Host: ", ip);

            newNode = new Node();
            newNode.progressBar = i;

            try {
                InetAddress inetAddress = InetAddress.getByName(ip);
                if (inetAddress.isReachable(timeout)) {
                    newNode.ip = ip;
                }

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return newNode;
    }

}






-- 
You received this message because you are subscribed to the Google Groups 
"Android Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-discuss.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-discuss/980df140-3cfd-4638-bc72-6813fa781e7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to