如何使用callables更新AsyncTask中的onProgressUpdate方法

时间:2017-03-29 15:51:37

标签: android-asynctask future executorservice

我想更新onProgressUpdate方法,但无法弄清楚如何。我的AsyncTask调用IpScanCallable类并使用ExecutorService和Future接口从中获取结果。但是,我不知道如何更新进度。 IpScanCallable接口返回Node类型的对象,因此返回onProgressUpdate方法。但是当我调用publishProgress(未来)它将无法正常工作。

如何更新结果?

如何填充ArrayList?

 /*
       * 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");
        }
    }

IpScanRunnable

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;
    }

}

0 个答案:

没有答案