【问题标题】:How to open another view in a widget that stays on the homescreen?如何在保留在主屏幕上的小部件中打开另一个视图?
【发布时间】:2018-12-23 02:10:33
【问题描述】:

我在使用 Guardian 新闻应用时注意到他们的小部件有一个选项按钮,可以打开另一个菜单。我不确定这是如何完成的。如果有人有任何想法,那就太好了。

Guardian Widget

Options menu open

【问题讨论】:

    标签: android android-intent android-widget


    【解决方案1】:

    这很容易打开对话框或模式弹出活动。

    【讨论】:

      【解决方案2】:

      您可以像往常一样发送Intent:

          public class YourWidget extends AppWidgetProvider {
      
          static PendingIntent getPendingSelfIntent(Context context, String action, int appWidgetId) {
              KLog.d(TAG, "getPendingSelfIntent appWidgetId = " + appWidgetId);
              Intent intent = new Intent(context, YourWidget.class);
              Bundle b = new Bundle();
              b.putInt("appWidgetId", appWidgetId);
              intent.putExtras(b);
              intent.setAction(action);
              PendingIntent pendInt = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
              return pendInt;
          }
      
          //....
      
          //your method for remote views 
          public void doUpdateView() {
               RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.collection_widget);
               remoteViews.setOnClickPendingIntent(R.id.button_settings, getPendingSelfIntent(context, RUN_CLICKED, appWidgetId));
         }
      
          @Override
          public void onReceive(final Context context, Intent intent) {
              super.onReceive(context, intent);
              if (RUN_CLICKED.equals(intent.getAction())) {
                  Intent actIntent = new Intent(context, YourActivity.class);
                  actIntent.putExtra("your_extra_key", your_extra_value);
                  actIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(actIntent);
              } 
              //...
              appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
          }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多