androidsynchronizationwear-osandroid-wear-data-api

Show data received by Wearable.DataApi when open the app


I am sending data to my Android Wear device from my phone using this code:

PutDataRequest putDataRequest = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient,putDataRequest);

On my Android Wear device, when the app is open, I use an override method onDataChanged(DataEventBuffer dataEventBuffer) and yes, the data that I receive is shown correctly.

But I need to receive data when my Android Wear app is closed, and show it when I open the app.

Anyone have a clue how to do this?

Thanks.

Kind regards.


Solution

  • Of course, you can receive data in any state. It doesn't matter if your app is open or not.

    Just put the following in your Android manifest:

    <service android:name=".YourService" >
        <intent-filter>
            <!-- listeners receive events that match the action and data filters -->
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
            <action android:name="com.google.android.gms.wearable.CHANNEL_EVENT" />
            <data android:scheme="wear" android:host="*" android:pathPrefix="/yourPathPrefix" />
        </intent-filter>
    </service>
    

    and add the following Java code:

    public class DataWearableListenerService extends WearableListenerService {
        @Override
        public void onDataChanged(DataEventBuffer dataEvents) {
            super.onDataChanged(dataEvents);
        }
        @Override
        public void onMessageReceived(MessageEvent messageEvent) {
        }
    }