无法更新android应用程序上的进度条

Posted

技术标签:

【中文标题】无法更新android应用程序上的进度条【英文标题】:unable to update progressbar on android app 【发布时间】:2017-08-01 03:30:10 【问题描述】:

伙计们,我正在研究端口扫描器。但由于某种原因,进度条没有更新。扫描仪启动后,我可以看到进度条变得可见;但是,没有更新。任何帮助表示赞赏 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);
        
    

进度条xml标签

<ProgressBar
        style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
        android:layout_
        android:layout_
        android:visibility="invisible"
        android:id="@+id/progress_bar" />

【问题讨论】:

【参考方案1】:

您不应将 max 设置为 0。将 max 设置为合理的值。

在 XML 中:

<ProgressBar
    style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
    android:layout_
    android:layout_
    android:visibility="invisible"
    android:id="@+id/progress_bar" 
    android:max="100"/>

在代码中:

progressBar.setMax(100);

【讨论】:

【参考方案2】:

问题是这一行:

progressBar.setMax(0);

来自Documentation

 void setMax (int max)

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

你应该把它改成progressBar.setMax(100);

【讨论】:

以上是关于无法更新android应用程序上的进度条的主要内容,如果未能解决你的问题,请参考以下文章

进度条与拖动条的使用学习

Android中通过实现线程更新ProgressDialog(对话进度条)

我需要知道已经上传了多少字节来更新进度条android

单击页面上的任意位置后,Android中的进度条未显示

android ListView嵌套ProgressButton更新进度条

Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)