代码之家  ›  专栏  ›  技术社区  ›  KevinTydlacka

Android移动/穿戴应用程序onMessageReceived多次调用一条消息

  •  4
  • KevinTydlacka  · 技术社区  · 10 年前

    我有一个基本的Android Wear应用程序,它成功地使用可穿戴数据层在移动设备和Wear之间发送和接收消息。

    然而,每当我(从移动或穿戴设备)发送消息时,另一端都会多次收到(比如5-17之间的任何地方)。为什么不只是收到一次?据我所知,它只发送一次。

    成功连接后,以下是我的 发送消息的代码 (两端相同):

    private void sendMsgToWear(final String strMsg){
    
        new AsyncTask<Void, Void, List<Node>>(){
    
            @Override
            protected List<Node> doInBackground(Void... params) {
                return getNodes();
            }
    
            @Override
            protected void onPostExecute(List<Node> nodeList) {
                for(Node node : nodeList) {
                    Log.v("DEVELOPER", "......Phone: Sending Msg: " + strMsg + " to node:  " + node.getId());
    
                    PendingResult<MessageApi.SendMessageResult> result = Wearable.MessageApi.sendMessage(
                            mGoogleApiClient,
                            node.getId(),
                            strMsg,
                            null
                    );
    
                    result.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                        @Override
                        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                            Log.v("DEVELOPER", "......Phone: " + sendMessageResult.getStatus().getStatusMessage());
                        }
                    });
                }
            }
        }.execute();
    
    
    }
    
    private List<Node> getNodes() {
        List<Node> nodes = new ArrayList<Node>();
        NodeApi.GetConnectedNodesResult rawNodes =
                Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
        for (Node node : rawNodes.getNodes()) {
            nodes.add(node);
            nodeID = node.getId();
        }
        return nodes;
    }
    

    这就是我 接收 在实现MessageApi.MessageListener的活动上:

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
    
        Log.d("DEVELOPER", "......Wear: successfully received phone to wear communication!");
    
    
    }
    

    发送端的输出如下:

    07-11 10:54:57.646  26920-28711/com.wearTest.message V/DEVELOPER﹕ ......Phone: Sending Msg: testMessage to node:  6ecc099a-60f8-4c3d-90fe-fa2f248c6bf7
    07-11 10:54:57.666  26920-26920/com.wearTest.message V/DEVELOPER﹕ ......Phone: null
    

    接收侧日志:

    07-11 10:54:57.962    5509-5672/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.962    5509-5610/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.962    5509-5660/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.962    5509-5521/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.962    5509-5539/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.972    5509-5570/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.972    5509-5604/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.972    5509-5601/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.972    5509-5566/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.972    5509-5520/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.972    5509-5582/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.972    5509-5567/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:57.982    5509-5587/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:58.002    5509-5581/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:58.052    5509-5581/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:58.052    5509-5587/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    07-11 10:54:58.062    5509-5570/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
    

    提前感谢!

    1 回复  |  直到 10 年前
        1
  •  2
  •   matiash    10 年前

    这个 MessageApi 应该工作正常。小心打电话 addListener() 只有一次,否则你会收到多次通知。

    或者,添加匹配项 removeListener() 调用(例如 onResume() / onPause() ).