【发布时间】:2016-10-27 03:19:37
【问题描述】:
目前我正在研究带有collection的app widget,有两点我很困惑,看下面的代码
关于更新代码
Intent serviceIntent=new Intent(context,StackService.class);
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.stack_widget);
remoteViews.setRemoteAdapter(R.id.stack_widget,serviceIntent);
remoteViews.setEmptyView(R.id.stack_widget,R.id.stack_empty);
Intent toastIntent = new Intent(context, StackWidget.class);
toastIntent.setAction(StackWidget.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.stack_widget, toastPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i],remoteViews);
getViewAt 代码
public RemoteViews getViewAt(int position) {
RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.stack_item);
remoteViews.setImageViewResource(R.id.item_image,R.raw.logo);
remoteViews.setTextViewText(R.id.item_text,list.get(position));
Bundle extras = new Bundle();
extras.putInt(StackWidget.EXTRA_ITEM, position);
Intent fillIntent=new Intent();
fillIntent.putExtras(extras);
remoteViews.setOnClickFillInIntent(R.id.item_button,fillIntent);
return remoteViews;
}
第一个问题是下面代码的目的是什么, serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))); 我通读了google api doc,但找不到答案。
第二个问题是关于toastIntent,为什么intent action被设置在onUpdate方法而不是getViewAt方法?为什么在按下按钮时会触发具有名为 TOAST_ACTION 的自定义操作的待处理意图?
感谢您提前回答我的问题。
【问题讨论】:
标签: android android-widget android-appwidget