简体   繁体   English

WearableListenerService不会在Wear上调用onDataChanged

[英]WearableListenerService don't call onDataChanged on Wear

I already tryed many ways to solve this, but nothing works. 我已经尝试了多种方法来解决此问题,但是没有任何效果。

making-wearablelistenerservice-ondatachanged-call-on-every-putdatamaprequest 使可穿戴式监听器服务在数据上更改每次调用数据映射请求

wearablelistenerservices-ondatachanged-not-called-on-phone 可穿戴式监听器服务,数据更改,不叫电话

AndroidManifest.xml AndroidManifest.xml

    <service android:name="my.package.ListenerService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />

            <data
                android:host="*"
                android:pathPrefix="/prefix"
                android:scheme="wear" />
        </intent-filter>
    </service>

MainActivity 主要活动

  IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
  MobillsWearActivity.MessageReceiver messageReceiver = new MobillsWearActivity.MessageReceiver();
  LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);

Service 服务

public class ListenerService extends WearableListenerService {

private final String TAG_WEAR = "[on-wear]";

@Override
public void onDataChanged(DataEventBuffer dataEvents) {


    for (DataEvent event : dataEvents) {

        // Check the data type
        if (event.getType() == DataEvent.TYPE_CHANGED) {

            DataMapItem dataItem = DataMapItem.fromDataItem (event.getDataItem());
            String jsonString = dataItem.getDataMap().getString("listDespesas");

            // Broadcast message to wearable activity for display
            Intent messageIntent = new Intent();
            messageIntent.setAction(Intent.ACTION_SEND);
            messageIntent.putExtra("listDespesas", jsonString);
            LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent);

            Log.i(TAG_WEAR, "Recebido no wearable: "+jsonString);
        }

    }
}

@Override
public void onMessageReceived(MessageEvent messageEvent) {
    Toast.makeText(this, "Received message", Toast.LENGTH_LONG).show();
}


}

The answer of Tom in this link works for me 汤姆在此链接中的答案对我有用

There are two things you can check: 您可以检查两件事:

  • packageName of the wear app should be identical to packageName of the device app. Wear应用程序的packageName应与设备应用程序的packageName相同。
  • onDataChanged() gets only called when the data really changes. 仅在数据真正更改时才调用onDataChanged() If you put the same data into the DataApi multiple times, the method is only called once until you write different data. 如果多次将相同的数据放入DataApi,则该方法仅被调用一次,直到您写入不同的数据为止。 You could add a timestamp to the data to make sure that the method gets called once for each write operation. 您可以在数据上添加时间戳,以确保为每个写入操作调用一次该方法。 However, using the DataApi is potentially more expensive in terms of battery usage than sending a message. 但是,就电池使用而言,使用DataApi可能比发送消息更昂贵。 So if you just want to trigger an action after putting data into the DataApi, simply send a message when you are done. 因此,如果您只想在将数据放入DataApi之后触发操作,则只需在完成后发送一条消息即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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