package com.example.android.droidscanner;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
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;
ProgressBar progressBar;
@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);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
progressBar.setMax(0);
//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();
progressBar.setClickable(false);
}
/*
* class to update progress bar during port scanning
*/
class Ports {
int progress;
String address;
}
private class PortScanTask extends AsyncTask<Void, Ports, Void> {
int timeOut = 1000; //for how long try connection in ms
Ports port;
@Override
protected void onPreExecute() {
openPorts.clear();
statusMsg.setText("Scanning " + ipAddress + " ports..");
progressBar.setVisibility(View.VISIBLE);
}
@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);
port = new Ports();
port.progress = i;
if (socket.isConnected()) {
socket.close();
port.address = address + ": " + i;
}
//sleep for 2 ms
SystemClock.sleep(200);
publishProgress(port);
} 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(Ports... values) {
if(values[0].address != null) {
openPorts.add(values[0].address);
portAdapter.notifyDataSetInvalidated();
Toast.makeText(getApplicationContext(), values[0].toString() + " open!", Toast.LENGTH_SHORT).show();
}
progressBar.setProgress(values[0].progress);
}
@Override
protected void onPostExecute(Void aVoid) {
statusMsg.setText("Done!.. ");
progressBar.setVisibility(View.GONE);
}
}
}
progressbar xml标签
<ProgressBar
style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:id="@+id/progress_bar" />
答案 0 :(得分:0)
您不应将diagnosis
设置为0.将max
设置为合理的。
在XML中:
max
在代码中:
<ProgressBar
style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:id="@+id/progress_bar"
android:max="100"/>
答案 1 :(得分:0)
问题在于这一行:
progressBar.setMax(0);
void setMax (int max)
将进度条的范围设置为0 ...最大
您应该将其更改为progressBar.setMax(100);