android - 使用 setPendingIntentTemplate 打开 Activity

标签 android android-intent android-pendingintent

我正在做一个带有一个 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/

相关文章:

android - 通知操作按钮不会触发未决 Intent

java - Android - 停止服务的通知挂起 Intent

android - UI - 如何同时显示 GridView 和 Linear 布局

android - 得到异常 : fragment already active

android - 我如何处理预览和人脸检测的旋转问题

android - 从应用程序内启动浏览器 - 如何返回应用程序?

java - Intent.FLAG_ACTIVITY_CLEAR_TASK 的替代方案

android - 计费库PurchasesResult不包括订阅的expiryTimeMillis,没有实时通知如何获取?

Android,MediaStore.ACTION_VIDEO_CAPTURE 使用了哪个摄像头? (正面或背面)

android - 通知中的待处理 Intent 不起作用