0

이것을 수행 할 수 있습니까? onDataChanged 작업을 수행 할 수 없습니다. complicationService입니다. WearableListener 클래스에서 수행하면 complicationService에 데이터를 전달하고 업데이트를 적용 할 수 있습니까? 대단히 감사합니다!dataApi를 사용하여 복잡성 업데이트

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입니다 : :이 complicationService

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? 
    } 
} 

}

입니다

이 코드는 변경 ocurrs 때 데이터 항목을 업데이트하는 핸드 헬드에 (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(); 

} 
+0

약간의 코드와 노력을 보여주십시오. 하지만 어쨌든 데이터 API가 변경되지 않는 한 데이터를 실제로 보내지는 않는다는 점을 명심하십시오. 따라서 일반적인 접근법은 타임 스탬프를 추가하는 것입니다. –

+0

죄송합니다. 게시물을 일부 코드로 업데이트했습니다. 타임 스탬프에 대해서는 읽었지만 데이터가 바뀌면 복잡성을 업데이트하기 만하면됩니다. –

+0

'AndroidManifest.xml'에 서비스를 올바르게 선언 했습니까? –

답변

0

WearableListenerServiceProviderUpdateRequester을 입력하고 싶습니다. 데이터베이스, SharedPreferences에 - 당신이 그렇게하기 전에 (시계에) 로컬에서 변경된 데이터를 저장해야합니다, 물론

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

: 핸드 헬드에서 변경된 데이터를받을 때,이 같은 전화를 걸 또는 무엇이든간에 - 귀하의 합병증 제공자에게 직접 데이터를 전달할 수있는 방법이 없기 때문입니다. 하지만 어쨌든 당신의 공급자를 일하게하기 위해서는 이미 그렇게하고있을 것입니다.

문서는 여기에서 있습니다 : https://developer.android.com/reference/android/support/wearable/complications/ProviderUpdateRequester.html

+0

예, ProviderUpdateRequester가 필요한 항목이었습니다. 감사! –