2017-03-08 44 views
0

我正在開發一個應用程序,其中包括執行端口掃描。我的問題是,我試圖在掃描發生時實施進度條。現在我使用AsynTask來執行掃描。但是如何在掃描過程中更新進度條?我可以爲這兩個任務使用相同的AsynTask,還是需要實施單獨的任務。任何幫助感謝! PortScanActivity我該如何一次運行後臺進程

package com.example.android.droidscanner; 

import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.widget.Adapter; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.IOException; 
import java.net.ConnectException; 
import java.net.InetAddress; 
import java.net.InetSocketAddress; 
import java.net.Socket; 
import java.net.SocketAddress; 
import java.net.SocketTimeoutException; 
import java.net.UnknownHostException; 
import java.util.ArrayList; 

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

public class PortScanActivity extends AppCompatActivity{ 

    String ipAddress; 
    ArrayList<String> openPorts; 
    ArrayAdapter<String> portAdapter; 
    int startPort = 0; 
    int endPort = 1023; 
    TextView statusMsg; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.scan_list); 

     Intent portIntent = getIntent(); 
     ipAddress = portIntent.getStringExtra("host"); 

     statusMsg = (TextView) findViewById(R.id.status_msg); 

     //setting the adapter 
     ListView portList = (ListView) findViewById(R.id.scan_list); 
     openPorts = new ArrayList<>(); 
     portAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, openPorts); 
     portList.setAdapter(portAdapter); 

     //scanning ports 
     PortScanTask portScan = new PortScanActivity.PortScanTask(); 
     portScan.execute(); 

    } 

    private class PortScanTask extends AsyncTask<Void, String, Void> { 

     int timeOut = 1000; //for how long try connection in ms 

     @Override 
     protected void onPreExecute() { 
      openPorts.clear(); 
      statusMsg.setText("Scanning " + ipAddress + " ports.."); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      for (int i = startPort; i <= endPort; i++) { 
       try { 
        //establishing connection to every port 
        Socket socket = new Socket(); 
        SocketAddress address = new InetSocketAddress(ipAddress, i); 
        socket.connect(address, timeOut); 
        Log.v("Connecting to: ", ipAddress + i); 
        if (socket.isConnected()) { 
         socket.close(); 
         publishProgress(ipAddress + ": " + i); 
        } 
       } catch (UnknownHostException e) { 
        e.printStackTrace(); 
       } catch (SocketTimeoutException e) { 
        e.printStackTrace(); 
       } catch (ConnectException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      return null; 
     } 

     @Override 
     protected void onProgressUpdate(String... values) { 
      openPorts.add(values[0]); 
      portAdapter.notifyDataSetInvalidated(); 
      Toast.makeText(getApplicationContext(), values[0].toString() + " open!", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      statusMsg.setText("Scan Complete!"); 
     } 
    } 
} 

回答

0

在進度更新通過自定義類對象,它包含兩種,字符串和進步整數。

class ProgressStep { 
    int progress; 
    String address; 
} 

private class PortScanTask extends AsyncTask<Void, ProgressStep, Void> { 

    int timeOut = 1000; //for how long try connection in ms 

    @Override 
    protected void onPreExecute() { 
     openPorts.clear(); 
     statusMsg.setText("Scanning " + ipAddress + " ports.."); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     for (int i = startPort; i <= endPort; i++) { 
      try { 
       //establishing connection to every port 
       Socket socket = new Socket(); 
       SocketAddress address = new InetSocketAddress(ipAddress, i); 
       socket.connect(address, timeOut); 
       Log.v("Connecting to: ", ipAddress + i); 
       int progress = count_your_progress; 
       ProgressStep step = new ProgressStep(); 
       step.progress = progress; 
       if (socket.isConnected()) { 
        socket.close(); 
        String address = ipAddress + ": " + i; 
        step.address = address; 
       } 
       publishProgress(step); 
      } catch (UnknownHostException e) { 
       e.printStackTrace(); 
      } catch (SocketTimeoutException e) { 
       e.printStackTrace(); 
      } catch (ConnectException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(ProgressStep... values) { 
     ProgressStep step = values[0]; 
     if (step.address != null) { 
      openPorts.add(step.address); 
      portAdapter.notifyDataSetInvalidated(); 
      Toast.makeText(getApplicationContext(), step.address + " open!", Toast.LENGTH_SHORT).show(); 
     } 

     progressBar.setProgress(step.progress); 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     statusMsg.setText("Scan Complete!"); 
    } 
} 
+0

但是進度發生在doInBackground中。例如,假設我正在掃描1000個端口。增量發生在doInBackground中。每次掃描一個新的端口時,我也會增加進度。我怎麼能通過進入progressUpdate值[0]只包含從端口掃描字符串...除非,我錯過了如何正確地做到這一點的概念。希望你能理解我的關注..謝謝 – miatech

+0

我編輯了我的帖子。檢查一下,可能會有用。 –