Mick Corp
Mick Corp

Reputation: 41

Android wear complication update on date change

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!

Upvotes: 4

Views: 1130

Answers (1)

Yuri Schimke
Yuri Schimke

Reputation: 13458

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

Upvotes: 0

Related Questions