发送消息到android设备,它将消息发送回android服装,但是收到的消息从未被触发

时间:2016-05-16 05:57:40

标签: android wear-os

我正在为android编写一个应用程序,它与设备进行通信并返回到磨损状态。这是onclick on wear的代码。

 message1Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE1_PATH, null).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                @Override
                public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                    Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
                    if (sendMessageResult.getStatus().isSuccess()) {

                        intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION);
                        intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.message1_sent));
                    } else {
                        intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION);
                        intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.error_message1));
                    }
                    startActivity(intent);
                }
            });
  

以下是手持设备上收到的on代码。

 public void onMessageReceived(MessageEvent messageEvent) {
            if (messageEvent.getPath().equals(MESSAGE1_PATH)) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        receivedMessagesEditText.append("\n" + getString(R.string.received_message1));
                        String serverURL = "A Working URL";
                        try {
                            ArrayList<String> list=new ArrayList<String>();
                            list.add(0,serverURL);
                            list.add(1,"SELECT DISTINCT ACCOUNTNO FROM CUSTOMR_WISE_WALLET_POINTS ORDER BY ACCOUNTNO");
                            jsonContent= new LongOperation().execute(list).get();
                        } catch (InterruptedException e) {
                            Log.e("Error",e.getMessage());
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        }

                        Log.d("Value of json",jsonContent);

                    }
                });
                Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE4_PATH, jsonContent.getBytes()).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                    @Override
                    public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                        if (sendMessageResult.getStatus().isSuccess())
                            Toast.makeText(getApplication(), "Message 4 sent", Toast.LENGTH_SHORT).show();
                        else
                            Toast.makeText(getApplication(), getString(R.string.error_message1), Toast.LENGTH_SHORT).show();
                    }
                });
            } else if (messageEvent.getPath().equals(MESSAGE2_PATH)) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        receivedMessagesEditText.append("\n" + getString(R.string.received_message2));
                    }
                });
            }
        }

手持设备显示&#34;消息4已发送&#34;敬酒。但是从来没有触发过磨损的消息监听器。代码是。

messageListener = new MessageApi.MessageListener() {
        @Override
        public void onMessageReceived(final  MessageEvent messageEvent) {
            Log.d("Inside","On message recieved");
            if (messageEvent.getPath().equals(MESSAGE1_PATH)) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        receivedMessagesEditText.append("\n" + getString(R.string.received_message1));
                        /*COde to send another request*/
                        Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE3_PATH, "Hello".getBytes()).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                            @Override
                            public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                                Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
                                if (sendMessageResult.getStatus().isSuccess()) {
                                    intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION);
                                    intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, "Message 3 sent");
                                } else {
                                    intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION);
                                    intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.error_message2));
                                }
                                startActivity(intent);
                            }
                        });


                    }
                });
            } else if (messageEvent.getPath().equals(MESSAGE2_PATH)) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        receivedMessagesEditText.append("\n" + getString(R.string.received_message2));
                    }
                });
            }
            else if(messageEvent.getPath().equals(MESSAGE4_PATH)){

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        receivedMessagesEditText.append("\n" + getString(R.string.received_message1));
                        /*COde to send another request*/

                       // String jsonInString=new String(messageEvent.getData());
                        Log.d("Inside Message 4","Inside");
                        String[] items = new String[]{"Select User", "2", "three"};
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, items);
                        dropdown.setAdapter(adapter);
                        message2Button.setEnabled(true);

                    }
                });

            }
        }
    };

总结一下,我可以通过磨损发送消息并在handhled上发送触发消息。消息已成功从手持设备发送,但是当从messagelistener范围内调用时,不会在磨损时触发接收到的消息。我检查了应用程序ID,它们是相同的。我也测试了通过触摸来自掌上电脑的按钮2收到的消息。它工作正常。提前致谢

1 个答案:

答案 0 :(得分:0)

MessageApi不保证即使返回成功的结果代码也会收到消息。查看文档here

  

注意:成功的结果代码不保证传递消息。如果您的应用需要数据可靠性,请使用DataItem对象或ChannelApi类在设备之间发送数据。