我想在连接到websocket时显示加载动画

时间:2015-04-30 11:26:47

标签: java android android-animation java-websocket

我正在尝试在连接到网络服务器时在我的应用中显示动画,这样用户就不会认为它已经崩溃/冻结。

以下是代码中可能相关的位:

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()中使用了上述方法。

2 个答案:

答案 0 :(得分: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"/>

然后在建立websocket连接后立即隐藏您的progressBar

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()调用之前显示。

P.S。我在这个例子中使用了这个java websocket库

dependencies {
    compile "org.java-websocket:Java-WebSocket:1.3.0"
}

答案 1 :(得分:0)

你可以在程序中使用j进度条同时使用计时器,这样用户就可以看到移动进度条......如果你想这样回复我,我会给你编码如何使用进度栏...