Wear OS Tiles 如何每秒更新一次?
How do Wear OS Tiles update themselve every second?
我制作了一个应该显示当前心率的磁贴。该磁贴从共享偏好中获取心率,并且运行良好,但我需要每秒刷新一次。目前,我只能使用 .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 秒。
@VisibleForTesting static final long MIN_INTER_UPDATE_INTERVAL_MILLIS = SECONDS.toMillis(20);
我制作了一个应该显示当前心率的磁贴。该磁贴从共享偏好中获取心率,并且运行良好,但我需要每秒刷新一次。目前,我只能使用 .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 秒。
@VisibleForTesting static final long MIN_INTER_UPDATE_INTERVAL_MILLIS = SECONDS.toMillis(20);