• <bdo id='FUi8p'></bdo><ul id='FUi8p'></ul>

    <small id='FUi8p'></small><noframes id='FUi8p'>

    <tfoot id='FUi8p'></tfoot>

    <legend id='FUi8p'><style id='FUi8p'><dir id='FUi8p'><q id='FUi8p'></q></dir></style></legend>
  • <i id='FUi8p'><tr id='FUi8p'><dt id='FUi8p'><q id='FUi8p'><span id='FUi8p'><b id='FUi8p'><form id='FUi8p'><ins id='FUi8p'></ins><ul id='FUi8p'></ul><sub id='FUi8p'></sub></form><legend id='FUi8p'></legend><bdo id='FUi8p'><pre id='FUi8p'><center id='FUi8p'></center></pre></bdo></b><th id='FUi8p'></th></span></q></dt></tr></i><div id='FUi8p'><tfoot id='FUi8p'></tfoot><dl id='FUi8p'><fieldset id='FUi8p'></fieldset></dl></div>

        在 Android Widget 上处理多个按钮单击

        时间:2023-09-08

          <small id='McMv6'></small><noframes id='McMv6'>

        1. <tfoot id='McMv6'></tfoot>

              <tbody id='McMv6'></tbody>
            <legend id='McMv6'><style id='McMv6'><dir id='McMv6'><q id='McMv6'></q></dir></style></legend>
              <bdo id='McMv6'></bdo><ul id='McMv6'></ul>
                1. <i id='McMv6'><tr id='McMv6'><dt id='McMv6'><q id='McMv6'><span id='McMv6'><b id='McMv6'><form id='McMv6'><ins id='McMv6'></ins><ul id='McMv6'></ul><sub id='McMv6'></sub></form><legend id='McMv6'></legend><bdo id='McMv6'><pre id='McMv6'><center id='McMv6'></center></pre></bdo></b><th id='McMv6'></th></span></q></dt></tr></i><div id='McMv6'><tfoot id='McMv6'></tfoot><dl id='McMv6'><fieldset id='McMv6'></fieldset></dl></div>

                2. 本文介绍了在 Android Widget 上处理多个按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我看到了这个话题并实现了IntentService正如所描述的那样,但是如果我想要更多的一个按钮怎么办?如何区分按钮?我正在尝试 setFlags,但无法在 onHandleIntent() 方法中读取它:

                  I saw this topic and implement IntentService as describes, but what if I want more that one button? How can I distinguish button from each other? I'm trying to setFlags, but cannot read it at onHandleIntent() method:

                  public static class UpdateService extends IntentService {
                  ...
                  
                      @Override
                      public void onHandleIntent(Intent intent) {
                          ComponentName me = new ComponentName(this, ExampleProvider.class);
                          AppWidgetManager manager = AppWidgetManager.getInstance(this);
                          manager.updateAppWidget(me, buildUpdate(this));
                      }
                  
                      private RemoteViews buildUpdate(Context context) {
                          RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.main_layout);
                  
                          Intent i = new Intent(this, ExampleProvider.class);
                          PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
                          updateViews.setOnClickPendingIntent(R.id.button_refresh, pi);
                  
                          i = new Intent(this, ExampleProvider.class); 
                          pi = PendingIntent.getBroadcast(context, 0, i, 0);
                          updateViews.setOnClickPendingIntent(R.id.button_about, pi);
                  
                          return updateViews;
                      }
                  }
                  

                  在这段小代码中,我有两个与 setOnClickPendingIntent 链接的 PendingIntent,我可以区分这个 Intent 用于不同的操作和处理吗?感谢您的帮助

                  At this little piece of code I have two PendingIntent linked with setOnClickPendingIntent, can I distinguish this intent for different actions and processing? Thanks for help

                  推荐答案

                  这行,out Widget class that extends AppWidgetProvider:

                  That works, out Widget class that extends AppWidgetProvider:

                  public class ExampleProvider extends AppWidgetProvider {
                  
                  // our actions for our buttons
                  public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh";
                  public static String ACTION_WIDGET_SETTINGS = "ActionReceiverSettings";
                  public static String ACTION_WIDGET_ABOUT = "ActionReceiverAbout";
                  
                  
                  @Override
                  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
                      RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main_layout);
                  
                      Intent active = new Intent(context, ExampleProvider.class);
                      active.setAction(ACTION_WIDGET_REFRESH);
                      PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
                      remoteViews.setOnClickPendingIntent(R.id.button_refresh, actionPendingIntent);
                  
                      active = new Intent(context, ExampleProvider.class);
                      active.setAction(ACTION_WIDGET_SETTINGS);
                      actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
                      remoteViews.setOnClickPendingIntent(R.id.button_settings, actionPendingIntent);
                  
                      active = new Intent(context, ExampleProvider.class);
                      active.setAction(ACTION_WIDGET_ABOUT);
                      actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
                      remoteViews.setOnClickPendingIntent(R.id.button_about, actionPendingIntent);
                  
                      appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
                  }
                  
                  @Override
                  public void onReceive(Context context, Intent intent) {
                      if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
                          Log.i("onReceive", ACTION_WIDGET_REFRESH);
                      } else if (intent.getAction().equals(ACTION_WIDGET_SETTINGS)) {
                          Log.i("onReceive", ACTION_WIDGET_SETTINGS);
                      } else if (intent.getAction().equals(ACTION_WIDGET_ABOUT)) {
                          Log.i("onReceive", ACTION_WIDGET_ABOUT);
                      } else {
                          super.onReceive(context, intent);
                      }
                  }
                  ...
                  

                  还有 AndroidManifest.xml,我们必须在其中注册我们的操作:

                  And AndroidManifest.xml where we must register our actions:

                      <receiver android:name="ExampleProvider">
                          <intent-filter>
                              <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
                              <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_REFRESH"/>
                              <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_SETTINGS"/>
                              <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_ABOUT"/>
                          </intent-filter>
                          <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/>
                      </receiver>
                  </application>
                  

                  这篇关于在 Android Widget 上处理多个按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:修改 Android seekbar 小部件以垂直操作 下一篇:如何在android中的通知中添加按钮?

                  相关文章

                  1. <tfoot id='7pMNW'></tfoot>

                    <small id='7pMNW'></small><noframes id='7pMNW'>

                    <i id='7pMNW'><tr id='7pMNW'><dt id='7pMNW'><q id='7pMNW'><span id='7pMNW'><b id='7pMNW'><form id='7pMNW'><ins id='7pMNW'></ins><ul id='7pMNW'></ul><sub id='7pMNW'></sub></form><legend id='7pMNW'></legend><bdo id='7pMNW'><pre id='7pMNW'><center id='7pMNW'></center></pre></bdo></b><th id='7pMNW'></th></span></q></dt></tr></i><div id='7pMNW'><tfoot id='7pMNW'></tfoot><dl id='7pMNW'><fieldset id='7pMNW'></fieldset></dl></div>
                      <bdo id='7pMNW'></bdo><ul id='7pMNW'></ul>
                    <legend id='7pMNW'><style id='7pMNW'><dir id='7pMNW'><q id='7pMNW'></q></dir></style></legend>