Kann dies getan werden? Ich kann nicht onDataChanged Arbeiten ist ComplicationService und wenn ich es in einer WearableListener-Klasse mache, wie kann ich die Daten an den ComplicationService übergeben und erzwingen ein Update? Vielen Dank!Verwenden von DataApi zur Aktualisierung der Komplikation
Dieser Code ist auf dem Handheld das Datenelement zu aktualisieren, wenn eine Änderung 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();
}
Dies ist die DataListener auf der Uhr:
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?
}
}
}
Diese complicationService ist (Google-Beispiel, das nur eine zufällige Zahl zur Verfügung stellt):
@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();
}
Bitte zeigen Sie uns einige Codes und Aufwand. Bedenken Sie jedoch, dass die Daten-API Ihre Daten nur dann sendet, wenn sie geändert werden. Ein allgemeiner Ansatz besteht darin, einen Zeitstempel hinzuzufügen. –
Entschuldigung, ich habe den Post mit etwas Code aktualisiert. Ich lese über den Zeitstempel, aber ich brauche ihn wirklich nur, um die Komplikation zu aktualisieren, wenn sich Daten ändern, also ist es in Ordnung. –
Haben Sie den Dienst in 'AndroidManifest.xml 'korrekt deklariert? –