我正在使用网络扫描程序,为此我使用AsyncTask类将扫描发送到后台。但是我在实现onProgressUpdate()方法时遇到了问题。我将类型为String的ArrayList传递给TaskScanNetwork类。该阵列将包含网络上所有生命主机的列表。任何帮助赞赏。感谢
package com.example.android.droidscanner;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button btnRead;
Button btnScan;
TextView textResult;
ListView listViewNode;
ArrayList<Node> listNote;
ArrayList<String> listNetwork;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRead = (Button)findViewById(R.id.readhost);
btnScan = (Button) findViewById(R.id.readnet);
textResult = (TextView)findViewById(R.id.result);
listViewNode = (ListView)findViewById(R.id.nodelist);
listNote = new ArrayList<>();
listNetwork = new ArrayList<>();
//initidating adapter for host only
ArrayAdapter<Node> adapterHost = new ArrayAdapter<Node>(
MainActivity.this,
android.R.layout.simple_list_item_1,
listNote);
listViewNode.setAdapter(adapterHost);
//initiating adapter for network scan
ArrayAdapter<String> adapterNetwork = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_list_item_1,
listNetwork);
listViewNode.setAdapter(adapterNetwork);
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TaskReadAddresses(listNote, listViewNode).execute();
}
});
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TaskScanNetwork(listNetwork, listViewNode).execute();
}
});
}
private class TaskReadAddresses extends AsyncTask<Void, Node, Void> {
ArrayList<Node> array;
ListView listView;
TaskReadAddresses(ArrayList<Node> array, ListView v){
listView = v;
this.array = array;
array.clear();
textResult.setText("querying...");
}
@Override
protected Void doInBackground(Void... params) {
readAddresses();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
textResult.setText("Done");
}
@Override
protected void onProgressUpdate(Node... values) {
listNote.add(values[0]);
((ArrayAdapter)(listView.getAdapter())).notifyDataSetChanged();
}
private void readAddresses() {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
Node thisNode = new Node(ip, mac);
publishProgress(thisNode);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
* Asynctask to scan the network
* */
private class TaskScanNetwork extends AsyncTask<Void, ArrayList<String>, Void> {
String network;
ListView listView;
String lowBoundIp;
ArrayList<String> allNetwork;
TaskScanNetwork(ArrayList<String> array, ListView v){
listView = v;
allNetwork = array;
textResult.setText("Scanning...");
}
@Override
protected Void doInBackground(Void... params) {
scanNetwork();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
textResult.setText("Done");
}
@Override
protected void onProgressUpdate(ArrayList<String>... values) {
allNetwork.add(values);
((ArrayAdapter)(listView.getAdapter())).notifyDataSetChanged();
}
private void scanNetwork() {
try {
InetAddress localHost = InetAddress.getLocalHost();
lowBoundIp = localHost.getHostAddress();
int lastDot = lowBoundIp.lastIndexOf(".");
String host = lowBoundIp.substring(0, lastDot);
Process ping;
for(int i = 1; i < 255; i++) {
host = host + "." + Integer.toString(i);
try {
ping = Runtime.getRuntime().exec("ping -n 1 " + host);
int returnVal = ping.waitFor();
if(returnVal == 0) {
publishProgress(allNetwork);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}