我正在做一个带有一个 stackView 的小部件,我正在使用 RemoteViewFactory 在其中添加位图(图像)。
我的问题是当我想为每个图像(我在 stackView 中)执行 onClickPendingIntent()
时。我知道我必须改用 setPendingIntentTemplate()
,但我不知道如何在按下某个小部件的图像时转到 Activity。
然后这就是我在小部件类的 onUpdate()
中创建待定 Intent 模板的内容:
Intent templateIntent = new Intent(Intent.ACTION_VIEW);
templateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent templatePendingIntent = PendingIntent.getActivity(
context, 0, templateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.widget_stack_view,
templatePendingIntent);
这就是我在 RemoteViewsFactory 类中用来处理待定 Intent 模板的内容:
public RemoteViews getViewAt(int index){
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.grid_item_layout);
rv.setImageViewBitmap(R.id.image, images.get(index).getImage());
Intent fillInIntent = new Intent();
fillInIntent.putExtra(Intent.EXTRA_TEXT, images.get(index).getTitle());
rv.setOnClickFillInIntent(R.id.image, fillInIntent);
return rv;
}
到目前为止,这个 fillInIntent.putExtra(Intent.EXTRA_TEXT, images.get(index).getTitle());
正在使用手机向我展示“使用完整的操作”,您可以选择您要打开的应用程序,但如果您看到我在 putExtra()
中输入的是一个字符串。但我想要的是打开一个 Activity,然后使用 putExtra()
传递一个位图图像 (images.get(index).getImage()
)
最佳答案
我弄清楚出了什么问题:
Intent templateIntent = new Intent(context, Activity_I_want_to_open.class);
templateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent templatePendingIntent = PendingIntent.getActivity(
context, 0, templateIntent, 0);
然后,我不需要 FLAG_UPDATE_CURRENT,因为我不想更新任何东西。
在 RemoteViews 端:
Intent fillInIntent = new Intent();
rv.setOnClickFillInIntent(R.id.image, fillInIntent);
我不需要 putExtra(),因为我没有向其他 Activity 发送数据。
关于android - 使用 setPendingIntentTemplate 打开 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30247464/