【发布时间】:2014-04-24 22:18:07
【问题描述】:
我已经实现了一个带有RemoteViewsService 的小部件,其中包含一个ListView 的项目,其中所有项目都可以点击以启动服务。简而言之,我在AppWidgetProvider.onUpdate() 中使用setPendingIntentTemplate,在RemoteViewsService.RemoteViewsFactory() 中使用setOnClickFillInIntent。
在大多数情况下,我得到了预期的行为。但是,当用完一些内存并返回尝试再次单击列表中的项目时有时单击列表中的项目时没有任何反应:服务未启动,并且没有提供触摸反馈。如果我有多个小部件,其中一个列表可能有问题,而其他列表则没有。
有人知道如何解决这个问题吗?
其他一些测试表明:
- 如果我在有问题的列表中滚动一段时间然后尝试再次单击,它会起作用!我的第一个想法是原因是 RemoteViewsFactory.getView 被调用并因此更新了待处理的意图,但是查看日志我可以看到没有输入这个方法。
- 我还登录了
RemoteViewsService.RemoteViewsFactory.onDestroy,看看问题的原因是否是它已被删除,但事实并非如此。 - 如果我设法调用
AppWidgetManager.updateAppWidget(以便 AppWidgetProvider.onUpdate 将运行)问题消失(我从我的主应用程序中调用 updateAppWidget)。
这三个观察结果似乎表明问题出在 AppWidgetManager 而不是 RemoteViewsFactory 中。
AppWidgetProvider.onUpdate:
@Override
public void onUpdate(Context iContext, AppWidgetManager iWidgetMgr,
int[] iWidgetIds){
Log.d(DbgU.getAppTag(), DbgU.getMethodName());
//Going through all widgets placed (could be more than one)
for(int i = 0; i < iWidgetIds.length; i++){
//Setting up the remote view service
Intent tmpRVServiceIntent = new Intent(iContext,
RemoteViewsServiceC.class);
tmpRVServiceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
iWidgetIds[i]);
tmpRVServiceIntent.setData(Uri.parse(tmpRVServiceIntent.toUri(
Intent.URI_INTENT_SCHEME)));
//Setting up the remote views
RemoteViews tmpRemoteViews = new RemoteViews(iContext.getPackageName(),
R.layout.widget);
tmpRemoteViews.setRemoteAdapter(R.id.widget_listview,
tmpRVServiceIntent);
tmpRemoteViews.setEmptyView(R.id.widget_listview,
R.id.widget_empty_view);
//Setting up the pending intent template (the id will be filled
//in later in RemoteViewsFactoryC.getViewAt())
Intent tmpTemplateIntent = new Intent(iContext,
LauncherServiceC.class);
tmpTemplateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
iWidgetIds[i]);
PendingIntent tmpPendingIntent = PendingIntent.getService(iContext,
0, tmpTemplateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
tmpRemoteViews.setPendingIntentTemplate(R.id.widget_listview,
tmpPendingIntent);
//Applying the update for the views
iWidgetMgr.updateAppWidget(iWidgetIds[i], tmpRemoteViews);
}
}
RemoteViewsService.RemoteViewsFactory.getViewAt:
@Override
public RemoteViews getViewAt(int inPosition) {
Log.v(DbgU.getAppTag(), DbgU.getMethodName()
+ ", inPosition = " + inPosition);
//Moving the cursor to the current position
mItemCursor.moveToPosition(inPosition);
//Extracting values from the database
String tmpName = mItemCursor.getString(
mItemCursor.getColumnIndexOrThrow(ItemTableM.COLUMN_NAME));
long tmpItemId = mItemCursor.getLong(
mItemCursor.getColumnIndexOrThrow(ItemTableM.COLUMN_ID));
Uri tmpItemUri = DatabaseU.getItemUriFromId(tmpItemId);
//Setting up the remote views object
RemoteViews retRemoteViews = new RemoteViews(
mContext.getPackageName(), R.layout.widget_listitem);
retRemoteViews.setTextViewText(R.id.widget_listitem_textView, tmpName);
//Adding action URI to the intent template which was set for all the
//list rows in WidgetProviderC.onUpdate
Intent tmpFillInIntent = new Intent();
tmpFillInIntent.setData(tmpItemUri);
retRemoteViews.setOnClickFillInIntent(R.id.widget_listitem_textView,
tmpFillInIntent);
return retRemoteViews;
}
这个问题困扰我很久了,非常感谢大家的帮助
【问题讨论】:
标签: android android-widget android-pendingintent remoteview android-remoteview