日期更改的Android磨损并发症更新

发布于 2025-02-13 23:05:17 字数 4139 浏览 0 评论 0原文

首先,我必须说,我在佩戴操作系统方面是新手(并且一般不了解Java编程的知识:()。

我想在日期更改时进行更新的复杂性(只要更改是在午夜或用户设置新日期时触发的)。 android:name =“ android.intent.action.date_changed”/> 在主码中的广播中,

下一步是复杂性(独立于任何手表),这是我被困的地方。 android.support.wearable.complications.update_period_seconds in intent-filter ,但是经过几天的搜索,我无法找到有关更新日期更改日期更改的广播消息的方法 数据。

并发症 方法从未被调用(通过在调试模式下的日志确认)。我还有一个类,可以用一个空的onReceive方法(仅是日志操作)来扩展“ broadcastreceiver

'能够捕获date_changed信息...

感谢您的帮助!

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

谢谢您的回复!

我已经尝试了建议,但仍然不起作用。我不知道失败的地方,因为解释很清楚...

到目前为止,我正在尝试在日期更改中显示一个随机数。

清单如下:

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

关于提供商:

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();
    }
}
}

最后是接收器:

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();
    }
}
}

调试没有显示接收器的日志消息...我不知道它是否值得注意,但是我在Android Studio中使用了模拟器。

非常感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥︴琐丶欲为 2025-02-20 23:05:17

您应该在清单中注册广播接收器。

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

然后发射并发症Datasourceupdaterequester更新。

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();
       }
   }
}

参见 https://developer.android.com/reference/androidx/wear/watchface/complications/datasource/complicationdatasourceupdaterequester

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文