4

我试图在连接到 Web 服务器时在我的应用程序中显示动画,以便用户不会认为它已崩溃/冻结。

这是代码中可能相关的部分:

private void waitForWebSocketConnect() {
    long start = System.currentTimeMillis();
    long end = start + 3*1000; // 3 seconds
    while (!mWebSocketClient.isOpen()) {
        try {
            Thread.sleep(200);
            if(System.currentTimeMillis() >= end){
                throw new InterruptedException();
            }
        } catch (InterruptedException e) {
            fatalError("WebSocket did not connect. Please try again.");
        }
    }
}

我认为这也可能有用:

private void connectWebSocket() {

    final Activity faActivity = super.getActivity();

    URI uri;
    try {
        String uriString;
        if(isRegistering){
            //uriString = "ws://app.touchtechpayments.com:80/reg";
            uriString = "wss://ec2-52-16-13-241.eu-west-1.compute.amazonaws.com/reg";
        } else {
            //uriString = "ws://app.touchtechpayments.com:80/trans";
            uriString = "wss://ec2-52-16-13-241.eu-west-1.compute.amazonaws.com/trans";
        }
        Log.d("uriString", uriString);
        uri = new URI(uriString);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        return;
    }

    mWebSocketClient = new WebSocketClient(uri) {
        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            this.progressBar.setVisibility(View.GONE);
            Log.d("Websocket", "Opened");
        }

        @Override
        public void onMessage(String s) {
            Log.d("Websocket", "Received message " + s);

            if(isRegistering) {
                confirmRegistration(s);
            } else {
                confirmTransaction(s);
            }

        }

        @Override
        public void onClose(int i, String s, boolean b) {
            Log.d("Websocket", "Closed " + s);

            if(!allDone) {

                if (triedTwice) {

                    final String printToast = "Error received: " + s + "\nPlease try again.";

                    faActivity.runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(context, printToast, Toast.LENGTH_LONG).show();
                            faActivity.getFragmentManager().popBackStack();
                        }
                    });

                } else {

                    Log.d("Websocket", "Trying for second time.");

                    triedTwice = true;
                    if (lastInputToServer != null) {
                        setupSSL();

                        connectWebSocket();
                        waitForWebSocketConnect();

                        mWebSocketClient.send(lastInputToServer);

                    }

                }
            }
        }

        @Override
        public void onError(Exception e) {
            this.progressBar.setVisibility(View.GONE);
            Log.d("Websocket", "Error " + e.getMessage());
        }
    };

    setupSSL();
    mProgressBar.setVisibility(View.VISIBLE);
    mWebSocketClient.connect();
}

webSocketClient 实际上并没有在 onCreate() 中定义,上面的方法仍然在 onCreate() 中使用。

4

2 回答 2

2

首先在你的layout.xml中定义一个 ProgressBar并使用indeterminate参数启用不确定模式:

<ProgressBar  
     android:id="@+id/progressBar"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:visibility="invisible" 
     android:indeterminate = "true"/>

然后progressBar在 websocket 连接建立后立即隐藏你:

public void onCreate(){

    [...]

    mProgressBar     = (ProgressBar) findViewById(R.id.progressBar);

    mWebSocketClient = new WebSocketClient(uri) {

        @Override
        public void onOpen(ServerHandshake serverHandshake){
            this.progressBar.setVisibility(View.GONE); // or INVISIBLE
        }
        @Override
        public void onMessage(String s) {

        }

        @Override
        public void onClose(int i, String s, boolean b) {
        }
        @Override
        public void onError(Exception e) {
            this.progressBar.setVisibility(View.GONE); // or INVISIBLE
        }
    };

    mProgressBar.setVisibility(View.VISIBLE);
    mWebSocketClient.connect();
}

您还可以将 ProgressBar 定义为不可见,并在稍后mWebSocketClient.connect()调用之前显示。

PS我在这个例子中使用这个java websocket库

dependencies {
    compile "org.java-websocket:Java-WebSocket:1.3.0"
}
于 2015-04-30T13:29:36.367 回答
0

您可以在程序中同时使用 j 进度条和计时器,这样用户就可以看到移动的进度条...如果您愿意,请回复我,我将为您提供如何使用进度条的编码.. .

于 2015-04-30T13:06:04.857 回答