我正在从穿戴式设备向手持设备发送一条消息。
第一次单击掌上电脑上的按钮只是调用服务的onCreate方法,第二次服务也收到消息。我不知道问题所在。

这是我的穿戴部分:

    @Override
    public void onClick(View v)
    {
        final PendingResult<NodeApi.GetConnectedNodesResult> nodes1 = Wearable.NodeApi.getConnectedNodes(mApiClient);
        nodes1.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>()
        {
            @Override
            public void onResult(NodeApi.GetConnectedNodesResult result)
            {
                for (int i = 0; i < result.getNodes().size(); i++)
                {
                    Node node = result.getNodes().get(i);

                    PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(mApiClient, node.getId(), "/OPEN;"
                            + cnt, null);
                    messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>()
                    {
                        @Override
                        public void onResult(MessageApi.SendMessageResult sendMessageResult)
                        {
                            Status status = sendMessageResult.getStatus();
                            Log.d("", "##Status: " + status.toString());
                            if (status.getStatusCode() != WearableStatusCodes.SUCCESS)
                            {
                                //not getting there, always success
                            }
                        }
                    });
                }
            }
        });
    }


我有以下WearableListenerService:

public class DataLayerListenerService extends WearableListenerService implements ConnectionCallbacks, OnConnectionFailedListener
{
    private GoogleApiClient mApiClient;
    private SharedPreferences prefs;

    @Override
    public void onCreate()
    {
        Log.d("", "##DataService created");
        super.onCreate();
        if (null == mApiClient)
        {
            mApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
                    .build();
        }

        if (!mApiClient.isConnected())
        {
            mApiClient.connect();
            Log.d("", "##Api connecting");
        }
    }

    @Override
    public void onDestroy()
    {
        Log.d("", "##DataService destroyed");
        if (null != mApiClient)
        {
            if (mApiClient.isConnected())
            {
                mApiClient.disconnect();
            }
        }
        super.onDestroy();
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent)
    {
        Log.d("", "##DataService received " + messageEvent.getPath());
        super.onMessageReceived(messageEvent);
        if (messageEvent.getPath().contains("/OPEN;"))
        {
            // only if called twice :(
        }
    }

    @Override
    public void onDataChanged(DataEventBuffer dataEvents)
    {
        Log.d("", "##DataService Data changed");
    }


    @Override
    public void onConnectionFailed(ConnectionResult arg0)
    {
    }

    @Override
    public void onConnected(Bundle arg0)
    {
    }

    @Override
    public void onConnectionSuspended(int arg0)
    {
    }
}


也许我还应该提到,在服务被销毁之前(单击事件发生几秒钟后),我只需要单击两次按钮。
所以..实际上这是发生了什么:


  穿点击
  
  服务创建
  
  穿点击
  
  服务onMessageReceived
  
  穿点击
  
  服务onMessageReceived
  
  //等待几秒钟
  
  服务破坏
  
  穿点击
  
  服务创建
  
  穿点击
  
  服务onMessageReceived


希望这很清楚:-)

提前致谢!!

最佳答案

我想我可能已经解决了它Phillip Adam-尝试在对超类进行调用的onMessageReceived()方法中注释掉服务中的这一行。这适合我。 :)

10-02 05:58