onProgressUpdate仅在AsyncTask结束时调用(套接字创建)

时间:2016-05-02 15:23:33

标签: android sockets android-asynctask

我正在尝试在套接字建立连接时显示“等待”对话框。你知道为什么这段代码不起作用吗?

onProgressUpdate在doInBackground的末尾被调用。我打算在connectionSocket.connect超时之前显示它。

以下位

dialog = connection.dialog("progress");
        dialog.show();

独立运作!

@Override
    protected Boolean doInBackground(String... ip) {
        Log.i("CONNECTION","doInBackground : Creating socket");
        Boolean result = false;
        try {
            publishProgress();
            connectionSocket = new Socket();
            connectionSocket.connect(new InetSocketAddress(ip[0], connection.getServerPort()), 5000);
            publishProgress();
            Log.i("CONNECTION","doInBackground : Socket created");
            result = true;
        } catch (UnknownHostException e) {
            Log.i("CONNECTION","doInBackground : Error creating socket. UnknownHostException");
        } catch (IOException ioe) {
            Log.i("CONNECTION","doInBackground : Error creating socket. IOException");
        }
        return result;
    }

   @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
        Log.i("CONNECTION","onProgressUpdate");
        dialog = connection.dialog("progress");
        dialog.show();
    }

感谢您的帮助:*

1 个答案:

答案 0 :(得分:1)

您需要在onPreExecute中显示对话框,而不是onProgressUpdate。 OnProgressUpdate将用于长时间操作,您可以对手头的任务进行特定的百分比更新。

wind.data
        wd variable           value
1    202.5       ws             0.4
2    135.0       ws             0.6
3    202.5       ws             0.6
4    180.0       ws             1.2
5    157.5       ws             0.6
6    112.5       ws             0.4
7    135.0       ws             0.6
8     90.0       ws             0.5
9     90.0       ws             0.4
10    90.0       ws               1
11   135.0       ws             2.7
12   135.0       ws             3.4
13   112.5       ws             1.7