javaandroidkotlinwear-osandroid-wear-complication

Wear OS complication is not being updated to the latest value


I am trying to update my complication from the shared SharedPreferences value which I define in my Activity like this

SharedPreferences.Editor editor = getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE).edit();
editor.putString("temp", wristSkinTempC);
editor.putString("tempF", wristSkinTempF);
editor.apply();

At first time of the complication it shows me the real data but when I tap on it and measure the new values it does not shows in the complication but the older value. It does get updated on its own after few mints sometime take even more time. but I want it to be updated as soon as there be new value inside the getSharedPreferences

Here is my complication code

private fun createAppShortcutComplication(): ComplicationData {

    val prefs = getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE)
    val tempC = prefs.getString("temp", "") //"No name defined" is the default value.

    val intent = Intent(this, MainActivity::class.java).apply {
        component = ComponentName(this@CustomComplicationC, MainActivity::class.java)
    }
    val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

    return ShortTextComplicationData.Builder(
            text = PlainComplicationText.Builder(text = tempC.toString()).build(),
            contentDescription = PlainComplicationText
                .Builder(text = "Short Text version of Number.").build(),
        ).setMonochromaticImage(
            MonochromaticImage.Builder(
                image = Icon.createWithResource(this, R.drawable.iconforbody),
            ).build(),
        ).setTapAction(pendingIntent)
        .build()
}

Here is my AndroidManifest

<meta-data        
  android:name="android.support.wearable.complications.SUPPORTED_TYPES"
  android:value="SHORT_TEXT" />
<meta-data
  android:name="android.support.wearable.complications.UPDATE_PERIOD_SECONDS"
  android:value="30" />

I have tried above code which does shows me the value but does not update immediately after measuring the new value.


Solution

  • You should request an update of all complications of a complication class once your app has new data using ComplicationDataSourceUpdateRequester:

            ComplicationDataSourceUpdateRequester
                .create(
                    context = context,
                    complicationDataSourceComponent = ComponentName(context, ComplicationClass::class.java)
                )
                .requestUpdateAll()
    
    Note

    You should set android.support.wearable.complications.UPDATE_PERIOD_SECONDS to 0, since the data never changes until we request an update, this will save battery:

            <meta-data
                android:name="android.support.wearable.complications.UPDATE_PERIOD_SECONDS"
                android:value="0" />`
    
    Learn More