android - 远程视图服务意图上的 setData
问题描述
有什么用
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
Intent
在AppWidgetProvider 类中的以下内容(参见下面的代码)
根据 CommonsWare 的书,原因是:
虽然您的应用程序可以访问您的 RemoteViewsService 类对象,但应用程序小部件主机不会,因此我们需要一些可以跨进程边界工作的东西。您可以选择将自己的添加到 RemoteViewsService 并使用基于此的 Intent,但这会使您的服务比您想要的更公开可见。
资料来源:忙碌的程序员 Android 开发指南 https://commonsware.com/Android/
但是当我删除这一行时,什么都没有改变。小部件仍然有效。
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
[...]
Intent serviceIntent = new Intent(context, WidgetService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(R.id.stack_view, serviceIntent);
views.setEmptyView(R.id.stack_view, R.id.empty_view);
[...]
appWidgetManager.updateAppWidget(appWidgetId, views);
}
解决方案
我想到了。如果不使用setData
,系统将无法区分不同的服务意图,导致它对onGetViewFactory
多个小部件只调用一次,并向所有小部件发送相同的意图。例如,如果您将应用小部件 id 发送给您的RemoteViewsService
和从他们发送给您的,RemoteViewsFactory
并将其显示在每个项目中,如果您不使用setData
.
推荐阅读
- r - 在 ggplot2 中绘制出处包的输出
- c++ - Why my UniqPtr objects are double in size compared to std::unique_ptr?
- c# - Json deserialization problem in c # form & json.net application
- firebase - Horizontal ListView inside SliverAppBar
- ajax - Click don't work well when i scroll, jScrollPane and ajax
- discord - Currency is not defined
- python - 如何在predict_proba python中获取单个项目
- jquery - 在vue js中单击父级时如何将类绑定到子级
- c++ - 用opengl模拟一辆汽车超车
- assembly - 在不使用 MOVZX/MOVSX 的情况下在汇编中进行零/符号扩展