简体   繁体   中英

Android Wear - receiving message from Wear without WearableListenerService?

I'm working on an app for Android Wear which can start an activity on the phone. Everything works great, but I'd like to not rely on a constantly running background service on the phone to pick up this message. Ideally, I would have the WearableListenerService start at the same time as the app and stay running until onPause.

Is there a way of intercepting a message from Wear with a broadcast receiver and intent filter, and then starting the app/service from the receiver? Here's some of the code:

To send a message to the phone:

private void sendMessage(String path, byte[] data) {
    if (mNode != null && mGoogleApiClient!=null && mGoogleApiClient.isConnected()) {
        Wearable.MessageApi.sendMessage(
                mGoogleApiClient, mNode.getId(), path, data).setResultCallback(
                new ResultCallback<MessageApi.SendMessageResult>() {
                    @Override
                    public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                        if (!sendMessageResult.getStatus().isSuccess()) {
                            Log.e("TAG", "Failed to send message with status code: "
                                    + sendMessageResult.getStatus().getStatusCode());
                            Toast info = Toast.makeText(WearActivity.this, "Failed to send   message, status code: " + sendMessageResult.getStatus().getStatusCode(), Toast.LENGTH_SHORT);
                            info.show();
                        }
                    }
                }
        );
    }
}

On the receiving end, I have WearableListenerService which receives a message. (Note, both phone and mobile have WearableListenerService classes so communication is 2-way.

Per the Sending an Syncing data training :

WearableListenerService (for services)

Extending WearableListenerService lets you listen for important data layer events in a service. The system manages the lifecycle of the WearableListenerService, binding to the service when it needs to send data items or messages and **unbinding the service when no work is needed.*

DataListener (for foreground activities)

Implementing DataListener in an activity lets you listen for important data layer events when an activity is in the foreground. Using this instead of the WearableListenerService lets you listen for changes only when the user is actively using your app.

If you only want to receive messages when the activity is open (ignoring any messages sent when it is closed), you can add a MessageApi.MessageListener to your activity.

If you want to receive messages whether your app is open or not (ensuring you don't drop messages sent from the Wearable), then you should use a WearableListenerService (note: WearableListenerService does not run constantly - it is only started when a new message/node connection/data layer change is sent and stopped when there are no more messages).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM