【问题标题】:Two questions about app widget with collection关于app widget with collection的两个问题
【发布时间】: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


    【解决方案1】:

    Intent.toUri() 可以将这个Intent 转换成一个String 持有它的URI 表示。

    setDate常用于未声明的Intent,可以启动与日期相关的组件。

    【讨论】:

    • 你能解释一下把setData放在这里的目的吗?
    • 不,我不会告诉你的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多