如何显示从Runnable到MainActivity的消息

时间:2015-02-03 10:13:16

标签: android

我正在使用外部库SignalR,我在Java中找到了Github代码我已成功实现它并接收日志消息,如Connected,Message等,但当我尝试在MainActivity EditText和textviews中显示这些消息时,它真的不起作用。以下是根据我的需要修改的代码现在告诉我如何在android中相应地修改以在Ui上接收消息。

public class HubClient  {
public HubProxy RelayServerHubProxy;
MainActivity mainActivity = new MainActivity();
public HubConnection RelayServerHubConnection;
Context context = null;
public Boolean Connected = false;
public static String ErrorName,ConnectionStatus,MessageReceived;

Logger logger = new Logger() {

    @Override
    public void log(String message, LogLevel level) {
        // TODO Auto-generated method stub
        // System.out.println(message);
        Log.v("Message Received in Logger", message);

    }
};

public HubClient(Context context) {
    this.context = context;
    mainActivity = new MainActivity();
}

public void Connect(String ServerURI, String SockConnectionType) {

    try {

        ClientTransport webSockTransport = null;

        RelayServerHubConnection = new HubConnection(ServerURI);

        // creating hub prox object
        RelayServerHubProxy = RelayServerHubConnection
                .createHubProxy("MyHub");

        // Start the connection

        RelayServerHubConnection.start().done(new Action<Void>() {

            @Override
            public void run(Void obj) throws Exception {
                // TODO Auto-generated method stub
                Log.v("Connection Status", "Connection done");

            }
        });

        // Subscribe to the error event

        RelayServerHubConnection.error(new ErrorCallback() {

            public void onError(Throwable error) {
                // TODO Auto-generated method stub
                error.printStackTrace();
                Log.v("WE've GOt erroe", error.getMessage());
                ErrorName = error.getMessage();
                //mainActivity.ShowToast(error.getMessage());


            }
        });

        // Subscribe to the connected event

        RelayServerHubConnection.connected(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                Log.v("Connected", "Connected");

                Connected = true;
                // Toast.makeText(mainActivity, "Connected",
                // Toast.LENGTH_LONG).show();

            }
        });

        // Subscribe to the closed event

        RelayServerHubConnection.closed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Log.v("Connection is", "Closed");


            }
        });

        RelayServerHubProxy.subscribe(new Object() {
            @SuppressWarnings("unused")
            public void messageReceived(String name, String message) {
                Log.v("Server Message", name + message);
                // Toast.makeText(context, message,
                // Toast.LENGTH_LONG).show();
                MessageReceived = name+message;

            }
        });

        // Subscribe to the received event

        RelayServerHubConnection.received(new MessageReceivedHandler() {

            @Override
            public void onMessageReceived(JsonElement json) {
                //how to show this message on again mainactivity Textview
                Log.v("onMessagReceived", json.toString());

            }
        });

        RelayServerHubConnection.stateChanged(new StateChangedCallback() {

            @Override
            public void stateChanged(ConnectionState oldState,
                    ConnectionState newState) {
                // TODO Auto-generated method stub
                if (newState == microsoft.aspnet.signalr.client.ConnectionState.Connected)

                {
                    // how to show Connected status in Textview?

                } else if (oldState == microsoft.aspnet.signalr.client.ConnectionState.Disconnected) {

                    // Show Message here
                }

            }
        });

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

}

public void SendMessageToServer(String messageToServer1) {

    try {

        RelayServerHubProxy.invoke("MessageFromClient",
                (String) messageToServer1);

        RelayServerHubConnection.error(new ErrorCallback() {

            public void onError(Throwable error) {
                // TODO Auto-generated method stub
                error.printStackTrace();
               //How to show message of error on Main Activity ?
            }
        });

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        Log.v("Exception", e.toString());
    }
}

}

  

现在我已经评论了我想要的功能。请注意,所有函数都使用Runnable。所以请告诉我如何修改它以在android

中使用

1 个答案:

答案 0 :(得分:0)

要显示alertDialog,请在HubClient对象中粘贴此函数:

private void showError(final String message) {
    ((Activity)context).runOnUiThread(new Runnable() {
        public void run() {
            new AlertDialog.Builder(context)
                .setTitle("Error")
                .setMessage(message)
                .setPositiveButton("Ok", null)
                .show();
        }
   });
}

您可以在错误回调中调用它

要在TextView中显示消息,请使用以下消息:

private void updateTextView(final String message) {
    ((Activity)context).runOnUiThread(new Runnable() {
        public void run() {
            // mTextView must be referenced by HubClient
            mTextView.setText(message);
        }
   });
}