4

First of all, I have to say that I am a a newbie in terms of Wear OS programming (and with very little knowledge in Java programming in general :( ).

I would like to make a complications that updates when the date changed (whenever the change is triggered at midnight or when the user sets a new date). I successfully manage to implement it in an activity with <action android:name="android.intent.action.DATE_CHANGED"/> in the Manifest and registering a BroadCastReceiver in the Main code.

The next step is the implementation as complications (independently of any watchface), and this is where I am stuck. I managed to write a complication updating periodically according to android.support.wearable.complications.UPDATE_PERIOD_SECONDS in intent-filter, but after days of searching I cannot find a way to catch the broadcast message regarding the date change for updating the complication data.

I tried to register a BroadCast receiver (like for the activity) in onComplicationActivated of the Provider but it seems that the method is never called (which is confirmed by the Log in debug mode). I also have a class that extents 'BroadcastReceiver' with an empty onReceive method (only log action), which also seems not called at all...

Hence, I am wondering what is the good method to be able to catch the Date_Changed information...

Thanks for your help!

====================================================================

Thanks for your reply!

I have tried as advised, but still not working. I don't get where I fail, as the explanations are clear...

So far I am trying to display a random number on date change.

The manifest is as follow:

<uses-feature android:name="android.hardware.type.watch" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:exported="true"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.DeviceDefault">

    <meta-data
        android:name="com.google.android.wearable.standalone"
        android:value="true" />

    <service
        android:name=".MickProviderService"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/complic_label"
        android:exported="true"
        android:permission="com.google.android.wearable.permission.BIND_COMPLICATION_PROVIDER">
        <intent-filter>
            <action android:name="android.support.wearable.complications.ACTION_COMPLICATION_UPDATE_REQUEST"/>
        </intent-filter>

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

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

    </service>

    <receiver android:name=".MickReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED" />
        </intent-filter>
    </receiver>

</application>

Regarding the Provider:

public class MickProviderService extends ComplicationProviderService {

@Nullable
@Override
public ComplicationData getPreviewData(@NonNull ComplicationType complicationType) {

    ComplicationData comp_data;
    final LongTextComplicationData.Builder builder = new LongTextComplicationData.Builder(
            new PlainComplicationText.Builder("coucou !").build(),
            new PlainComplicationText.Builder("Long Text=.").build()
    );
    comp_data = builder.build();

    return comp_data;
}


@Override
public void onComplicationRequest(@NonNull ComplicationRequest complicationRequest, @NonNull ComplicationRequestListener complicationRequestListener) {
    Log.d("MickProviderService", "Request");

    double num = new Random().nextInt(61) + 20;
    String text = String.valueOf(num);

    ComplicationData comp_data;
    final LongTextComplicationData.Builder builder = new LongTextComplicationData.Builder(
            new PlainComplicationText.Builder(text).build(),
            new PlainComplicationText.Builder("Long Text=.").build()
    );

    comp_data = builder.build();


    //envoi de la donnée au système
    try {
        complicationRequestListener.onComplicationData(comp_data);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
}

And finally the receiver:

public class MickReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent i) {
    Log.d("MickFeteReceiver", "DATE CHANGE");
    if (i.getAction().equals(Intent.ACTION_DATE_CHANGED)){

        ComponentName component = new ComponentName(context.getApplicationContext(),MickProviderService.class);

        ComplicationDataSourceUpdateRequester request = ComplicationDataSourceUpdateRequester.create(
                context, component);
        request.requestUpdateAll();
    }
}
}

The debug does not show the log message of the receiver... I don't know if it's noteworthy, but I am using an emulator in Android Studio.

Thanks a lot for your help!

TofferJ
  • 4,678
  • 1
  • 37
  • 49
Mick Corp
  • 41
  • 3

1 Answers1

0

You should register your broadcast receiver in the manifest.

<receiver android:name=".Receiver">
    <intent-filter>
        <action android:name="android.intent.action.DATE_CHANGED" />
    </intent-filter>
</receiver>

Then fire a ComplicationDataSourceUpdateRequester update.

public class Receiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent i) {
       if (i.getAction().equals(Intent.ACTION_DATE_CHANGED) {
            ComplicationDataSourceUpdateRequester request = ComplicationDataSourceUpdateRequester.create(
                applicationContext, ComponentName(
                    applicationContext, RememberWearComplicationProviderService.class
                )
            );
            request.requestUpdateAll();
       }
   }
}

See https://developer.android.com/reference/androidx/wear/watchface/complications/datasource/ComplicationDataSourceUpdateRequester

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69
  • 2
    Thanks a lot! I tried the implementation as advised, but as it was not working I am wondering whether or not it is related to a known issue with DATE_CHANGED (https://stackoverflow.com/questions/31787778/android-is-android-intent-action-date-changed-triggered-at-device-reboot). Changing DATE_CHANGED to TIME_SET indeed triggers the receiver with the associated action… What is strange is that with the activity (so not complications) it works, with the only difference being that the intent registration is done in the class, not the manifest. – Mick Corp Jul 09 '22 at 21:00