无法在Android应用上更新进度条

时间:2017-03-10 17:25:28

标签: android android-asynctask android-progressbar

我正在研究端口扫描仪。但由于某种原因,进度条没有更新。一旦扫描仪启动,我就可以看到进度条变得可见;但是,没有更新。任何帮助赞赏 PortScannerActivity

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" />

2 个答案:

答案 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);

来自Documentation

 void setMax (int max)
  

将进度条的范围设置为0 ...最大

您应该将其更改为progressBar.setMax(100);