使用 dataApi 更新并发症
Using dataApi to update complication
这能做到吗?我无法让 onDataChanged 工作是 complicationService,如果我在 WearableListener class 中这样做,我如何将数据传递给 complicationService 并强制更新?
非常感谢!
此代码在手持设备上,用于在发生更改时更新数据项:
private void syncDataItem(){
Log.d(TAG, "PanicAlert AlarmStatus=" + isAlertActive(context));
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(PATH_ALARM);
putDataMapRequest.getDataMap().putBoolean(FIELD_ALARM_ON, isAlertActive(context));
putDataMapRequest.setUrgent();
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataMapRequest.asPutDataRequest()).await();
}
这是手表上的 DataListener:
public class DataLayerListenerService extends WearableListenerService {
private static final String TAG = "DataLayerSample";
private static final String PATH_ALARM = "/alarm_path";
private static final String FIELD_ALARM_ON = "alarm_on";
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "onDataChanged: " + 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;
}
for (DataEvent event : dataEvents) {
//Force update of complication? How?
}
}
}
这是 complicationService(Google 示例只是提供一个随机数):
@Override
public void onComplicationUpdate(
int complicationId, int dataType, ComplicationManager complicationManager) {
Log.d(TAG, "onComplicationUpdate() id: " + complicationId);
if (mGoogleApiClient == null){
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
int randomNumber = (int) Math.floor(Math.random() * 10);
String randomNumberText =
String.format(Locale.getDefault(), "%d!", randomNumber);
// Create Tap Action so that the user can trigger an update by tapping the complication.
Intent updateIntent =
new Intent(getApplicationContext(), UpdateComplicationDataService.class);
updateIntent.setAction(UpdateComplicationDataService.ACTION_UPDATE_COMPLICATION);
updateIntent.putExtra(UpdateComplicationDataService.EXTRA_COMPLICATION_ID, complicationId);
PendingIntent pendingIntent = PendingIntent.getService(
getApplicationContext(),
complicationId,
updateIntent,
0);
ComplicationData complicationData = null;
switch (dataType) {
case ComplicationData.TYPE_SHORT_TEXT:
complicationData = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT)
.setShortText(ComplicationText.plainText(String.valueOf(actualState)))
.setTapAction(pendingIntent)
.build();
break;
if (complicationData != null) {
complicationManager.updateComplicationData(complicationId, complicationData);
}
//onStartWearableActivityClick();
}
在我看来你想要 ProviderUpdateRequester
在你的 WearableListenerService
中。当您从掌上电脑收到改变的数据时,像这样调用:
new ProviderUpdateRequester(context, new ComponentName(context, "myComplicationClass"))
.requestUpdateAll();
显然,在执行此操作之前,您需要将更改后的数据存储在本地(在手表上)- 在数据库中,SharedPreferences
,或其他任何地方 - 因为无法直接将数据传递给您的并发症提供者。但您可能已经在这样做,以使您的提供者无论如何都能正常工作。
这能做到吗?我无法让 onDataChanged 工作是 complicationService,如果我在 WearableListener class 中这样做,我如何将数据传递给 complicationService 并强制更新? 非常感谢!
此代码在手持设备上,用于在发生更改时更新数据项:
private void syncDataItem(){
Log.d(TAG, "PanicAlert AlarmStatus=" + isAlertActive(context));
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(PATH_ALARM);
putDataMapRequest.getDataMap().putBoolean(FIELD_ALARM_ON, isAlertActive(context));
putDataMapRequest.setUrgent();
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataMapRequest.asPutDataRequest()).await();
}
这是手表上的 DataListener:
public class DataLayerListenerService extends WearableListenerService {
private static final String TAG = "DataLayerSample";
private static final String PATH_ALARM = "/alarm_path";
private static final String FIELD_ALARM_ON = "alarm_on";
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "onDataChanged: " + 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;
}
for (DataEvent event : dataEvents) {
//Force update of complication? How?
}
}
}
这是 complicationService(Google 示例只是提供一个随机数):
@Override
public void onComplicationUpdate(
int complicationId, int dataType, ComplicationManager complicationManager) {
Log.d(TAG, "onComplicationUpdate() id: " + complicationId);
if (mGoogleApiClient == null){
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
int randomNumber = (int) Math.floor(Math.random() * 10);
String randomNumberText =
String.format(Locale.getDefault(), "%d!", randomNumber);
// Create Tap Action so that the user can trigger an update by tapping the complication.
Intent updateIntent =
new Intent(getApplicationContext(), UpdateComplicationDataService.class);
updateIntent.setAction(UpdateComplicationDataService.ACTION_UPDATE_COMPLICATION);
updateIntent.putExtra(UpdateComplicationDataService.EXTRA_COMPLICATION_ID, complicationId);
PendingIntent pendingIntent = PendingIntent.getService(
getApplicationContext(),
complicationId,
updateIntent,
0);
ComplicationData complicationData = null;
switch (dataType) {
case ComplicationData.TYPE_SHORT_TEXT:
complicationData = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT)
.setShortText(ComplicationText.plainText(String.valueOf(actualState)))
.setTapAction(pendingIntent)
.build();
break;
if (complicationData != null) {
complicationManager.updateComplicationData(complicationId, complicationData);
}
//onStartWearableActivityClick();
}
在我看来你想要 ProviderUpdateRequester
在你的 WearableListenerService
中。当您从掌上电脑收到改变的数据时,像这样调用:
new ProviderUpdateRequester(context, new ComponentName(context, "myComplicationClass"))
.requestUpdateAll();
显然,在执行此操作之前,您需要将更改后的数据存储在本地(在手表上)- 在数据库中,SharedPreferences
,或其他任何地方 - 因为无法直接将数据传递给您的并发症提供者。但您可能已经在这样做,以使您的提供者无论如何都能正常工作。