路过小部件ID到活动 [英] Passing widget id to activity

查看:97
本文介绍了路过小部件ID到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的部件供应商,我有以下的code:

In my widget provider, I have the following code:

for (int widget_id : appWidgetIds) {
  Log.d(TAG, "Widget Id: " + widget_id);
  RemoteViews remoteViews;

  Widget widget;

  while (true) {
    try {
      widget = Widget.load(context, Widget.class, widget_id);
      break;
    }
    catch (Exception ex) {
      Log.e(TAG, "Exception!", ex);
    }
  }

  // Widget is assigned to user
  if (widget != null) {
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_with_user);
    remoteViews.setCharSequence(R.id.user_text, "setText", widget.user.name);
    remoteViews.setUri(R.id.avatar, "setImageURI", Uri.parse(widget.user.avatar));
  }
  else {
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    Intent configIntent = new Intent(context, ConfigureWidget.class);
    configIntent.setAction(ACTION_WIDGET_CONFIGURE);
    configIntent.putExtra("widget_id", widget_id);

    PendingIntent configPendingIntent = PendingIntent.getActivity(context, REQUEST_CODE_ONE, configIntent, 0);
    remoteViews.setOnClickPendingIntent(R.id.configure_text, configPendingIntent);
  }

  appWidgetManager.updateAppWidget(widget_id, remoteViews);
}

在其他部分,我推出了配置活动,并试图将小部件ID传递给它。然而,包始终为null:

In the else section, I'm launching a configuration activity and trying to pass the widget id to it. However, the bundle is always null:

Bundle extras = getIntent().getExtras();

if (extras != null) {
  int widget_id = extras.getInt("widget_id");
}

我怎样才能获得小部件ID的推出活动?

How can I get the widget id to the launched activity?

推荐答案

的问题是,Android为与PendingIntents缓存。解决的办法是增加,导致其以更新缓存的PendingIntent,在FLAG_UPDATE_CURRENT标志

The issue was that android does caching with PendingIntents. The solution was to add the FLAG_UPDATE_CURRENT flag which causes it to update the cached PendingIntent.

PendingIntent configPendingIntent = PendingIntent.getActivity(context, REQUEST_CODE_ONE, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);

<一个href=\"http://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity\">Source

这篇关于路过小部件ID到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆