0

可以这样做吗?我无法得到onDataChanged工作是complicationService,如果我在WearableListener类中执行它,我如何将数据传递到complicationService并强制更新? 非常感谢!使用dataApi更新并发症

此代码是在手持设备上更改时ocurrs更新数据项:

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 (谷歌示例只是提供一个随机数):

@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(); 

} 
+0

请给我们一些代码和努力。但无论如何,请记住,数据API不会实际发送数据,除非它们发生更改。所以一般的做法是在其中添加一个时间戳。 –

+0

对不起,我更新了一些代码的帖子。我读了时间戳,但我真的只需要它来更新数据更改时的复杂情况,因此没关系。 –

+0

您是否在'AndroidManifest.xml'中正确声明了该服务? –

回答

0

这听起来像你想在你的WearableListenerServiceProviderUpdateRequester。当您从手持设备接收更改的数据,做出这样的呼吁:

new ProviderUpdateRequester(context, new ComponentName(context, "myComplicationClass")) 
     .requestUpdateAll(); 

很明显,你必须这样做之前本地存储(上表),更改的数据 - 在数据库中,SharedPreferences,或者其他 - 因为没有办法直接将数据传递给你的并发症提供者。但是,您可能已经在做这件事,让您的提供者无论如何工作。

文档在这里:https://developer.android.com/reference/android/support/wearable/complications/ProviderUpdateRequester.html

+0

是的,ProviderUpdateRequester正是我所需要的。谢谢! –