首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WearableListenerService的onDataChanged未被电话呼叫

WearableListenerService的onDataChanged未被电话呼叫
EN

Stack Overflow用户
提问于 2016-04-05 17:07:49
回答 1查看 760关注 0票数 0

我有一个应用程序上的可穿戴,应该发送一个数据到手持按钮点击。我做了几乎相同的设置从手持到电话,唯一的区别是,我发送了一条消息,这是完美的,现在我想发送一个DataMap的另一种方式。

我非常肯定,我有正确的设置,但仍然onDataChanged()上的wearableListenerService上的电话从来没有打过电话。我是不是忘了什么重要的事情,或者还有什么不对劲?

请看一看,拯救我的一天!

下面是从可穿戴设备发送数据的线程(从可穿戴设备的mainActivity调用,googleClient存在,我已经启动了线程)。调试成功发送数据that的返回。

代码语言:javascript
复制
public class SendDataMapToHandheldDataLayer_Thread extends Thread {

private String path;
private DataMap dataMap;
private GoogleApiClient googleClient;

public SendDataMapToHandheldDataLayer_Thread(String cPath, DataMap cDataMap, GoogleApiClient cGoogleClient){
    path = cPath;
    dataMap = cDataMap;
    googleClient = cGoogleClient;
}


public void run(){
    PutDataMapRequest putDMR = PutDataMapRequest.create(path);
    putDMR.getDataMap().putAll(dataMap);
    PutDataRequest request = putDMR.asPutDataRequest();
    DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleClient, request).await();
    if(result.getStatus().isSuccess()){
        Log.v("dataMapSender_Wear", "DataMap successfully sent!");
    }else{
        Log.v("dataMapSender_Wear", "ERROR: Failed to send DataMap to data layer");
    }


}


}

这是电话里的听众,从来没有人打过电话。为什么?从本质上说,它只是从安卓开发人员教程中复制而来:http://developer.android.com/training/wearables/data-layer/events.html#Listen。我也尝试过自己的版本,但我认为问题在其他地方。

代码语言:javascript
复制
public class ListenerServiceMobile extends WearableListenerService{



private static final String TAG = "DataLayerSample";
private static final String START_ACTIVITY_PATH = "/start-activity";
private static final String DATA_ITEM_RECEIVED_PATH = "/data-item-received";

@Override
public void onDataChanged(DataEventBuffer dataEvents) {

    Toast.makeText(getApplicationContext(), "Data changed!", Toast.LENGTH_LONG).show();


    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "onDataChanged: " + dataEvents);
    }
    final List<DataEvent> events = FreezableUtils
            .freezeIterable(dataEvents);

    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();

    ConnectionResult connectionResult =
            googleApiClient.blockingConnect(30, TimeUnit.SECONDS);

    if (!connectionResult.isSuccess()) {
        Log.e(TAG, "Failed to connect to GoogleApiClient.");
        return;
    }

    // Loop through the events and send a message
    // to the node that created the data item.
    for (DataEvent event : events) {
        Uri uri = event.getDataItem().getUri();

        // Get the node id from the host value of the URI
        String nodeId = uri.getHost();
        // Set the data of the message to be the bytes of the URI
        byte[] payload = uri.toString().getBytes();

        // Send the RPC
        Wearable.MessageApi.sendMessage(googleApiClient, nodeId,
                DATA_ITEM_RECEIVED_PATH, payload);
    }
}

移动的清单文件包含以下内容:

代码语言:javascript
复制
<service android:name=".ListenerServiceMobile">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED"></action>
            <data android:scheme="wear" android:host="*" android:pathPrefix="/prefix" />
        </intent-filter>
    </service>

(谢谢你的答覆!)

EN

回答 1

Stack Overflow用户

发布于 2016-04-13 23:41:01

我首先要删除android:pathPrefix,因为它正在过滤您的服务正在侦听的数据。您现在应该看到onDataChange()正在被调用。

在看到数据之后,可以返回并将pathPrefix设置为在可穿戴设备中设置路径的任何内容。(我猜/数据-项目收到)。pathPrefix匹配主机之后指定的任何内容的开头。

这是你想要的,如果是这样的话:

代码语言:javascript
复制
<service android:name=".ListenerServiceMobile">
  <intent-filter>
      <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
      <data android:scheme="wear" android:host="*"
               android:pathPrefix="/data-item-received"" />
  </intent-filter>
</service>
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36432795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档