【问题标题】:Widget OnClickPendingIntent Not Received By Service服务未收到小部件 OnClickPendingIntent
【发布时间】:2012-11-08 22:11:44
【问题描述】:

由于本网站的限制,我无法发布与我的问题相关的所有内容。 因此,您可以阅读以下所有内容:http://www.dreamincode.net/forums/topic/299148-widget-onclickpendingintent-not-received-by-service/

基本上,我的音乐应用程序有一个小部件,它显示当前正在播放的歌曲,并且应该允许用户切换播放或更改曲目。小部件更新良好,但服务未收到服务。

这就是我设置 OnClickPendingIntent 的方式:

  RemoteViews updateViews = null;
  ComponentName thisWidget = new ComponentName(this, GMWidget.class);
  updateViews = new RemoteViews(this.getPackageName(), R.layout.widget);
  Intent WIDGET_PAUSE_PLAY_intent = new Intent(this, GMService.class);
  WIDGET_PAUSE_PLAY_intent.setAction(WIDGET_PAUSE_PLAY);
  PendingIntent WidgetPausePlay = PendingIntent.getService(this, 0, new Intent(WIDGET_PAUSE_PLAY).setComponent(thisWidget), 0);
  updateViews.setonclickPendingIntent(R.id.pbWidMediaPlay, WidgetPausePlay);

这是我的 OnReceive:

public void onReceive(Context context, Intent intent) {
  //  super.onReceive(context, intent);
    String action = intent.getAction();
    Log.d(TAG,"CLICKK: " + action);
}

这是我的服务的意图过滤器:

<action android:name="android.media.AUDIO_BECOMING_NOISY" /> />
<action android:name="com.my.player.WIDGET_NEXT" />
<action android:name="com.my.player.WIDGET_PREV" />
<action android:name="com.my.player.WIDGET_PAUSE_PLAY" />
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />

【问题讨论】:

    标签: android service android-widget android-pendingintent


    【解决方案1】:

    您使用 getService() 创建的 PendingIntent 不会触发 onReceive()。您需要在 onStartCommand() 中收听它。

    【讨论】:

      【解决方案2】:

      不要忘记调用更新。这是来自我的一个应用程序的完整工作代码。更改它并按您认为合适的方式使用它。

      public class Widget extends AppWidgetProvider 
      {
      
          public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
                  int[] appWidgetIds) 
          {
      
              Log.i("Widget", "onUpdate");
      
              final int N = appWidgetIds.length;
      
              for (int i=0; i<N; i++) 
              {
      
                  int appWidgetId = appWidgetIds[i];
      
                  Log.i("Widget", "onUpdateLoop");
      
                  Intent intent = new Intent(context.getApplicationContext(), MyService.class);
                  intent.setAction(Long.toString(System.currentTimeMillis()));
      
                  PendingIntent pendingIntent = PendingIntent
                          .getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      
                  RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
      
                  views.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);
      
                  appWidgetManager.updateAppWidget(appWidgetId, views);
      
          }
      
        }
      
      }
      

      编辑:此外,如果您从应用程序中的任何其他位置更新您的小部件,请确保不要错过更新中的任何组件。对 updateAppWidget 的每次调用都必须包含所有相同的组件,尤其是任何 onPendingIntentClick

      【讨论】:

        猜你喜欢
        • 2017-06-27
        • 2012-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-26
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        相关资源
        最近更新 更多