在小部件上设置 TextView 时启动器卡顿
基本上,过了一会儿我的小部件开始导致启动器卡顿。在进行了一些分析、记录、注释以及良好的旧式目视检查之后,我已经缩小了问题范围以更新小部件文本。这是我用来执行此操作的方法:
public static void setText(Context context, String text) {
if (appWidgetManager == null)
appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = Widget.getView(context);
ComponentName thisWidget = new ComponentName(context, Widget.class);
remoteViews.setTextViewText(R.id.PrimaryWidgetButton, text);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
奇怪的是,需要几分钟才能开始导致滞后,就好像此代码存在某种挥之不去的副作用一样。删除小部件可以阻止卡顿,即使仍在调用此代码。重新添加小部件会立即恢复卡顿。
注意:仅当文本更改时才会出现卡顿现象。因此,在更改文本后,启动器很好,但在调整过程中它会卡顿。
是什么原因导致这种口吃?有没有更好的方法来更新小部件上的文本?
Basically, after a while my widget starts to cause the Launcher to stutter. After doing some profiling, logging, commenting out, and just good old visual inspection, I have narrowed down the problem area to updating the widget text. This is the method I am using to do this:
public static void setText(Context context, String text) {
if (appWidgetManager == null)
appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = Widget.getView(context);
ComponentName thisWidget = new ComponentName(context, Widget.class);
remoteViews.setTextViewText(R.id.PrimaryWidgetButton, text);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
The odd thing is, it takes a few minutes before it starts to cause the lag, as if there's some sort of lingering side-effect from this code. Removing the widget stops the stuttering, even though this code is still being called. Re-adding the widget resumes the stuttering immediately.
Note: The stuttering only occurs when the text is changed. So after it changes the text, the launcher is fine, but during the adjustment it stutters.
What could be causing this stuttering? Is there a better way to update text on a widget?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论