我正在研究网络扫描程序应用程序,它在IpScanActivity中的AsyncTask上给出了WifiManager类和方法getSystemService方法的错误。我在主要活动上使用了相同的类没有问题,但是当我在IpScanActivity上再次使用它时抱怨。我想将WifiInfo从主要活动传递给IpScanActivity,但还没有找到。我想通过实例化IpScanactivity(不知道这是否是这样做的方式)无论如何。为什么我在IpScanActivity上获得getSystemService。提前致谢。 主要活动
public class MainActivity extends AppCompatActivity {
Button btnReadNet;
TextView textResult;
ListView listViewNode;
ArrayList<String> networkList;
ArrayAdapter<String> adapterNet;
public String ip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnReadNet = (Button) findViewById(R.id.read_network);
textResult = (TextView) findViewById(R.id.result);
listViewNode = (ListView) findViewById(R.id.nodelist);
networkList = new ArrayList<>();
adapterNet = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, networkList);
listViewNode.setAdapter(adapterNet);
WifiManager wifiMan = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMan.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
ip = String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
textResult.setText(printOutput(wifiInfo));
//starting network scanner activity on button press
btnReadNet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent scanNetIntent = new Intent(MainActivity.this, IpScanActivity.class);
startActivity(scanNetIntent);
}
});
}
public String printOutput(WifiInfo wifiInfo) {
return "Local IP: " + ip + "\n" +
"SSID: " + wifiInfo.getSSID() + "\n" +
"BSSID: " + wifiInfo.getBSSID() + "\n" +
"MAC: " + wifiInfo.getMacAddress() + "\n" +
"Signal: " + wifiInfo.getRssi() + "db" + "\n" +
"Speed: " + wifiInfo.getLinkSpeed() + "Mbps" + "\n" +
"Frequency: " + wifiInfo.getFrequency() + "MHz" + "\n" +
"Connection: " + wifiInfo.getSupplicantState();
}
}
IpScanActivity类
public class IpScanActivity extends AppCompatActivity {
String ip;
String mac;
ArrayList<Node> hostList;
ListView networkScan;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scan_list);
hostList = new ArrayList<>();
//inflating adapter
networkScan = (ListView) findViewById(R.id.scan_list);
NodeAdapter networkAdapter = new NodeAdapter(this, R.layout.list_item, hostList);
networkScan.setAdapter(networkAdapter);
//scanning network
TaskScanNetwork scanNetwork = new TaskScanNetwork();
scanNetwork.execute();
}
/*
* 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> {
String localIp;
String mac;
String subnet;
static final int lower = 1;
static final int upper = 254;
static final int timeout = 1000;
@Override
protected void onPreExecute() {
WifiManager wifiMan1 = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo1 = wifiMan1.getConnectionInfo();
int ipAddress = wifiInfo1.getIpAddress();
localIp = String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
Log.v("Ip Address: ", localIp);
//subnet = ip.substring(0, ip.lastIndexOf("."));
hostList.clear();
//textResult.setText("Scanning Network...");
}
@Override
protected Void doInBackground(Void... params) {
for (int i = lower; i <= upper; i++) {
ip = subnet + "." + i;
Log.v("Host: ", ip);
try {
InetAddress inetAddress = InetAddress.getByName(host);
if (inetAddress.isReachable(timeout)) {
Node newNode = new Node(ip);
publishProgress(newNode);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Node... values) {
hostList.add(values[0]);
networkScan.deferNotifyDataSetChanged();
}
@Override
protected void onPostExecute(Void aVoid) {
//result.setText("Live Hosts: ");
}
}
}
节点类
public class Node {
String ip;
String mac;
String CanonicalHostName;
String HostName;
String remark;
boolean isReachable;
Node(String ip){
this.ip = ip;
//this.mac = mac;
this.isReachable = true;
}
public String getIp() {
return ip;
}
public String getHostName() {
return HostName;
}
public String getMac() {
return mac;
}
public String getRemark() {
return remark;
}
public boolean isReachable() {
return isReachable;
}
@Override
public String toString() {
return "IP: " + ip + "\n" +
"MAC: " + mac + "\n" +
"CanonicalHostName:\t" + CanonicalHostName + "\n" +
"HostName:\t" + HostName + "\n" +
"isReachable: " + isReachable +
"\n" + remark;
}
private void queryHost(){
try {
InetAddress inetAddress = InetAddress.getByName(ip);
CanonicalHostName = inetAddress.getCanonicalHostName();
HostName = inetAddress.getHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
remark = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
remark = e.getMessage();
}
}
}
NodeAdapter类
public class NodeAdapter extends ArrayAdapter<Node>{
public NodeAdapter(Context context, int num, ArrayList<Node> allHost) {
super(context, 0, allHost);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Node host = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
// Lookup view for data population
TextView nodeIpTxtView = (TextView) convertView.findViewById(R.id.node_ip);
TextView nodeMacTxtView = (TextView) convertView.findViewById(R.id.node_mac);
// Populate the data into the template view using the data object
nodeIpTxtView.setText(host.getIp());
nodeMacTxtView.setText(host.getMac());
// Return the completed view to render on screen
return convertView;
}
}