Wear OS瓷砖是如何每秒自我更新的?

我已经做了一个瓷砖,应该可以显示当前的心率。磁贴从共享的偏好中获取心率,它工作得很好,但我需要像每一秒一样刷新它。目前我只能使用.setFreshnessIntervalMillis(1000*60)

每分钟刷新一次

文档说我可以使用此命令从应用程序中的任何位置更新我的磁贴...

TileService.getUpdater(context)
       .requestUpdate(HeartRateTileService.class);

但这没有任何作用。我在几个地方尝试了它,比如在我的MainActivity中的onItemSelected中,在BroadCastReceiver中和在前台服务中。这是背景问题还是什么?我也试过

HeartRateTileService.getUpdater(this/getApplicationContext())
       .requestUpdate(HeartRateTileService.class);

这是我的磁贴...

public class HeartRateTileService extends TileService {
    private static final String RESOURCES_VERSION = "1";

    private String currentHeartrate = "0";
    private PrefManager prefManager;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int i = super.onStartCommand(intent, flags, startId);

        Log.d("HeartRate", "onStartCommand " + prefManager.getHeartrate());

        return i;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        prefManager = new PrefManager(this);
        Log.d("HeartRate", "onCreate " + prefManager.getHeartrate());
    }

    @NonNull
    @Override
    protected ListenableFuture<TileBuilders.Tile> onTileRequest(
            @NonNull RequestBuilders.TileRequest requestParams
    ) {
        Log.d("HeartRate", "onTileRequest " + prefManager.getHeartrate());
        return Futures.immediateFuture(new TileBuilders.Tile.Builder()
                .setResourcesVersion(RESOURCES_VERSION)
                .setFreshnessIntervalMillis(1000*60)
                .setTimeline(new TimelineBuilders.Timeline.Builder()
                        .addTimelineEntry(new TimelineBuilders.TimelineEntry.Builder()
                                .setLayout(new LayoutElementBuilders.Layout.Builder()
                                        .setRoot(
                                                layoutHeartRate()
                                        ).build()
                                ).build()
                        ).build()
                ).build()
        );
    }

    private LayoutElementBuilders.LayoutElement layoutPlay() {
        return new LayoutElementBuilders.Box.Builder()
                .setWidth(wrap())
                .setHeight(expand())
                .setVerticalAlignment(LayoutElementBuilders.VERTICAL_ALIGN_CENTER)
                .addContent(new LayoutElementBuilders.Row.Builder()
                        .addContent(
                                IconButton.buildIconButton(
                                        this,
                                        "ic_play",
                                        R.color.colorAccent,
                                        "start measuring heart rate",
                                        new ModifiersBuilders.Clickable.Builder()
                                                .setOnClick(new ActionBuilders.LaunchAction.Builder()
                                                        .setAndroidActivity(new ActionBuilders.AndroidActivity.Builder()
                                                                .setClassName(MainActivity.class.getName())
                                                                .setPackageName(this.getPackageName())
                                                                .build()
                                                        ).build()
                                                )
                                                .build()
                                )
                        ).build()
                )
                .build();
    }

    private LayoutElementBuilders.LayoutElement layoutHeartRate() {
        prefManager = new PrefManager(this);
        currentHeartrate = prefManager.getHeartrate();
        return new LayoutElementBuilders.Box.Builder()
                .setWidth(wrap())
                .setHeight(expand())
                .setVerticalAlignment(LayoutElementBuilders.VERTICAL_ALIGN_CENTER)
                .addContent(new LayoutElementBuilders.Row.Builder()
                        .addContent(
                                IconButton.buildIconButton(
                                        this,
                                        "",
                                        R.color.colorAccent,
                                        "start measuring heart rate",
                                        new ModifiersBuilders.Clickable.Builder()
                                                .setOnClick(new ActionBuilders.LaunchAction.Builder()
                                                        .setAndroidActivity(new ActionBuilders.AndroidActivity.Builder()
                                                                .setClassName(MainActivity.class.getName())
                                                                .setPackageName(this.getPackageName())
                                                                .build()
                                                        ).build()
                                                )
                                                .build()
                                )
                        ).build()
                )
                .addContent(new LayoutElementBuilders.Text.Builder()
                        .setText(prefManager.getHeartrate())
                        .build()
                )
                .build();
    }

    @NonNull
    @Override
    protected ListenableFuture<ResourceBuilders.Resources> onResourcesRequest(
            @NonNull RequestBuilders.ResourcesRequest requestParams
    ) {
        Log.d("HeartRate", "onResourcesRequest " + prefManager.getHeartrate());
        return Futures.immediateFuture(new ResourceBuilders.Resources.Builder()
                .setVersion(RESOURCES_VERSION)
                .addIdToImageMapping("ic_play", new ResourceBuilders.ImageResource.Builder()
                        .setAndroidResourceByResId(new ResourceBuilders.AndroidImageResourceByResId.Builder()
                                .setResourceId(R.drawable.ic_play)
                                .build()
                        ).build()
                )
                .build()
        );
    }

}

解决方案

最短时间为20秒。

参见https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:wear/tiles/tiles-renderer/src/main/java/androidx/wear/tiles/manager/UpdateScheduler.java;l=31?q=20%20f:androidx%2Fwear%2Ftiles&start=51

    @VisibleForTesting static final long MIN_INTER_UPDATE_INTERVAL_MILLIS = SECONDS.toMillis(20);

相关文章