Java 类com.google.android.gms.wearable.MessageApi.MessageListener 实例源码

项目:shoppinglist    文件:WearableConnectivity.java   
private synchronized void onClientConnected(final WearableEvents wearableEvents)
{
    if (!isConnected)
    {
        isConnected = true;

        threadPool.execute(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    PendingResult<Status> pendingResult = Wearable.MessageApi.addListener(apiClient, new MessageListener()
                    {
                        @Override
                        public void onMessageReceived(MessageEvent messageEvent)
                        {
                            String nodeId = messageEvent.getSourceNodeId();
                            String path = messageEvent.getPath();
                            byte[] payload = messageEvent.getData();

                            final Message message = new Message(nodeId, path, payload);

                            Handler handler = new Handler(Looper.getMainLooper());
                            handler.post(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    wearableEvents.onMessageReceived(message);
                                }
                            });
                        }
                    });
                    pendingResult.setResultCallback(new ResultCallback<Status>()
                    {
                        @Override
                        public void onResult(Status status)
                        {
                            if (status.isSuccess())
                            {
                                wearableEvents.onConnectedSuccess();
                            }
                            else
                            {
                                wearableEvents.onConnectedFail();
                            }
                        }
                    });
                }
                catch (Exception e)
                {
                    wearableEvents.onConnectedFail();
                }
            }
        });
    }
}
项目:tpgwear    文件:WearableConnectivity.java   
private synchronized void onClientConnected(final WearableEvents wearableEvents)
{
    if (!isConnected)
    {
        isConnected = true;

        threadPool.execute(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    PendingResult<Status> pendingResult = Wearable.MessageApi.addListener(apiClient, new MessageListener()
                    {
                        @Override
                        public void onMessageReceived(MessageEvent messageEvent)
                        {
                            String nodeId = messageEvent.getSourceNodeId();
                            String path = messageEvent.getPath();
                            byte[] payload = messageEvent.getData();

                            final Message message = new Message(nodeId, path, payload);

                            Handler handler = new Handler(Looper.getMainLooper());
                            handler.post(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    wearableEvents.onMessageReceived(message);
                                }
                            });
                        }
                    });
                    pendingResult.setResultCallback(new ResultCallback<Status>()
                    {
                        @Override
                        public void onResult(Status status)
                        {
                            if (status.isSuccess())
                            {
                                wearableEvents.onConnectedSuccess();
                            }
                            else
                            {
                                wearableEvents.onConnectedFail();
                            }
                        }
                    });
                }
                catch (Exception e)
                {
                    wearableEvents.onConnectedFail();
                }
            }
        });
    }
}