gpt4 book ai didi

android - Android Wear 上的通知有效,但数据层无效

转载 作者:行者123 更新时间:2023-11-29 20:58:39 24 4
gpt4 key购买 nike

我正在开发一款包含一些可穿戴功能的 Android 应用。我有一些使用 Wea​​rableExtender 的通知,它们工作正常。但是,当我尝试使用 Data Layer Api 时,它不起作用。

我使用了这篇文章的答案中提出的代码:Android Wear Watchface Settings on host但永远不会调用 onDataChanged(DataEventBuffer dataEvents)。我在手机和 watch 上使用 Android 模拟器。

这是我在 watch 的 LogCat 上得到的:

11-10 05:43:44.777: D/DataItems(1333): inserted data item row 60 for DataItemRecord
[es.example.rebeca.prueba,10b8f01e736f2a1276b2bbf41a6c6dd18c005e65,DataItemInternal
[f702125c, dataSz=65, host=db03afd0-746e-48ad-8b0d-98ff360bf672, path=/SAMPLE, numAssets=0],
db03afd0-746e-48ad-8b0d-98ff360bf672,seqId=13136,assetsAreReady=false]

watch 似乎收到了具有适当路径 (path=/SAMPLE) 的内容。但是,我看不到任何消息(我放了一些日志来检查数据是否到达 watch )。

如有任何提示,我们将不胜感激。

编辑:

我在手机端使用的代码:

public class MainActivity extends Activity {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
Log.d("DataLayerApi", "onConnected()"); //This is shown
}
@Override
public void onConnectionSuspended(int cause) { }
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {}
})
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}

public void sendWearable(View v) { //This is a button
syncSampleDataItem();
}

private void syncSampleDataItem() {
if(mGoogleApiClient == null)
return;

final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE");
final DataMap map = putRequest.getDataMap();
map.putInt("color", Color.RED);
map.putString("string_example", "Sample String");
Wearable.DataApi.putDataItem(mGoogleApiClient, putRequest.asPutDataRequest());
}
}

AndroidManifest.xml 中:

<uses-feature android:name="android.hardware.type.watch" />

我在可穿戴端使用的代码:

public class ListenerService extends WearableListenerService { 

String myTag = "DataLayerApi";

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
super.onDataChanged(dataEvents);
Log.d(myTag, "onDataChanged()" + dataEvents); //This is NEVER shown

final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
for(DataEvent event : events) {
final Uri uri = event.getDataItem().getUri();
final String path = uri!=null ? uri.getPath() : null;
if("/SAMPLE".equals(path)) {
final DataMap map = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
// read your values from map:
int color = map.getInt("color");
String stringExample = map.getString("string_example");
Log.d(myTag, color + stringExample); //This is NEVER shown
}
}
}

AndroidManifest.xml 中:

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

两个 Manifests 我有一行:

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

在应用标签内

最佳答案

所以事实证明,您需要在可穿戴和手持模块中使用SAME 包名称。我更改了它,现在代码可以正常工作了!

关于android - Android Wear 上的通知有效,但数据层无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26845503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com