我已经实现了一个小部件,RemoteViewsService
其中包含一个ListView
项目,其中所有项目都可以单击以启动服务。简而言之,我setPendingIntentTemplate
在我的AppWidgetProvider.onUpdate()
和setOnClickFillInIntent
我的RemoteViewsService.RemoteViewsFactory()
.
在大多数情况下,我得到了预期的行为。但是,当用完一些内存并返回尝试再次单击列表中的项目时,有时单击列表中的项目时没有任何反应:服务没有启动,也没有给出触摸反馈。如果我有几个小部件,其中一个列表可能有问题,而其他列表则没有。
有谁知道如何解决这个问题?
其他一些测试表明:
- 如果我在有问题的列表中滚动一段时间,然后尝试再次单击,它可以工作!我的第一个想法是原因是调用了 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;
}
我已经有这个问题很长时间了,非常感谢任何帮助