【问题标题】:onProgressUpdate only called at the end of AsyncTask (socket creation)onProgressUpdate 仅在 AsyncTask 结束时调用(创建套接字)
【发布时间】:2016-05-02 15:23:33
【问题描述】:

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

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();
    }

感谢您的帮助:*

【问题讨论】:

    标签: android sockets android-asynctask


    【解决方案1】:

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

    @Override
    protected void onPreExecute() {
        dialog = connection.dialog("progress");
        dialog.show();
    }
    

    【讨论】:

    • 谢谢,当然,这是有道理的......仍然有一些用户界面问题,但它按预期工作!
    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 2013-11-11
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    相关资源
    最近更新 更多