使用 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:

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

}

这是复杂的服务(谷歌的例子只是提供一个随机数(:

@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();
}
在我看来

,你想要在你的WearableListenerService中有一个ProviderUpdateRequester.当您从手持设备收到更改的数据时,请拨打如下电话:

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

显然,在这样做之前,您需要将更改的数据存储在本地(在手表上(中 - 在数据库中,SharedPreferences 或其他任何内容中 - 因为无法直接将数据传递给您的复杂提供商。但您可能已经在这样做,以使提供商无论如何都能正常工作。

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

相关内容

  • 没有找到相关文章

最新更新