未从BroadcastReceiver收到消息到WearableListerService

时间:2015-01-18 15:50:01

标签: android service broadcastreceiver wear-os receiver

我正在尝试将移动应用中的消息发送到可穿戴应用中的服务。 消息需要从BroadcastReceiver发送。在BroadcastReceiver中,消息被成功发送,至少是日志中所说的,但是没有触发onMessageReceive方法。 我不太确定问题是什么,我在build.gradle文件中都有相同的applicationId,但是包名称不同。 在移动清单中,接收者具有以下声明:

 <receiver android:name="com.app.mobile.NewAlarm"
            android:exported="false">
            <intent-filter>
                <action android:name="com.example.android.support.wearable.notifications.ACTION_EXAMPLE" />
            </intent-filter>
        </receiver>

BroadcastReceiver类:

    public class TimeAlarm extends BroadcastReceiver  {
        public static final String TAP_ACTION_PATH = "/tap";
         private static final String TAG = "NotificationReceiver";

         public void onReceive(Context context, Intent paramIntent) {
         mGoogleApiClient = new GoogleApiClient.Builder(context)
                        .addApi(Wearable.API)
                        .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                            @Override
                            public void onConnected(Bundle bundle) {
                                                }

                            @Override
                            public void onConnectionSuspended(int cause) {
                                                    }
                        }).build();
                mGoogleApiClient.connect();
               sendMessage();


                if (mGoogleApiClient.isConnected()) {
                    mGoogleApiClient.disconnect();
                }
        }

            private void getNodes() {
                Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(
                        new ResultCallback<NodeApi.GetConnectedNodesResult>() {
                            @Override
                            public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
                                HashSet<String> results = new HashSet<String>();
                                for (Node node : getConnectedNodesResult.getNodes()) {
                                    results.add(node.getId());
                                    Log.d(TAG,node.getId().toString());
                                }
                                Log.d(TAG,results.toString());
                                sendMessageApi(results);
                            }
                        }
                );
            }

            private void sendMessageApi(Collection<String> nodes) {
                for (String node : nodes) {
                    Wearable.MessageApi.sendMessage(
                            mGoogleApiClient, node, TAP_ACTION_PATH, null).setResultCallback(
                            new ResultCallback<MessageApi.SendMessageResult>() {
                                @Override
                                public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                                    if (!sendMessageResult.getStatus().isSuccess()) {
                                        Log.e(TAG, "ERROR: failed to send Message: " + sendMessageResult.getStatus());
                                    } else {
                                        Log.e(TAG, "Success!");
                                    }

                                }
                            }
                    );
                }
            }

            private void sendMessage() {
                getNodes();
            }
}

WearableListenerService在wear app manifest中以这种方式声明:

<service android:name=".DataLayerListenerService" >
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
            </intent-filter>
        </service>

最后是DataLayerListenerService:

public class DataLayerListenerService extends WearableListenerService {

    public static final String START_ACTIVITY_PATH = "/notice";
    public static final String TAP_ACTION_PATH = "/tap";
    private static final String TAG = "DataLayerListenerService";

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {

        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
         Log.d(TAG, "Message Received !!");
        if (START_ACTIVITY_PATH.equals(messageEvent.getPath())) {
            Log.d(TAG, "Message Received !!");

            int vibeTime = 5000;
            String strVibeTime = new String(messageEvent.getData());
            if (!strVibeTime.equals("") && !strVibeTime.equals("0")) {
                vibeTime = (Integer.parseInt(strVibeTime)) * 1000;
            }
            sleep(1000);
            vibrator.vibrate(vibeTime);

        } else if (TAP_ACTION_PATH.equals(messageEvent.getPath())) {
            Log.d(TAG, "Tapping Received !!");
            vibrator.cancel();
        }
    }

    public synchronized void sleep(long msec) {
        try
        {
            wait(msec);
        }catch(InterruptedException e){
            Log.e(TAG, e.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您尝试在连接客户端之前发送消息,因此getConnectedNodesResult.getNodes()返回一个空列表。

请改为尝试:

public void onReceive(Context context, Intent paramIntent) {
    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addApi(Wearable.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    sendMessage();
                }
                @Override
                public void onConnectionSuspended(int cause) {

                }
    }).build();
    mGoogleApiClient.connect();
}