1 Replies - 1658 Views - Last Post: 08 November 2012 - 04:06 PM

#1 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Widget OnClickPendingIntent Not Received By Service

Posted 07 November 2012 - 01:24 PM

Hi!
I have a music-app that has a Service that handles the musicplayer.
I've added a Widget and I'm updating it accordingly.
But i can't for my life, get the "Buttons"(It's really ImageView's) on the Widget to work (Prev, Pause/Play and Next)
I've set the onclickPendingIntents for each of the "Buttons", and i can hear that the Click is working (Normal clicking-sound when taping something), but my receiver do not get the broadcasts.

This is how i set the onclickPendingIntent:
	public void SetWidgetListener() {
			Log.d(TAG, "SetListener called");
		 	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);
		    
		    Intent Widget_Next_intent = new Intent(this, GMService.class);
		    Widget_Next_intent.setAction(WIDGET_NEXT);
		    PendingIntent WidgetNext = PendingIntent.getBroadcast(this, 0, new Intent(WIDGET_NEXT).setComponent(thisWidget), 0);
		    updateViews.setonclickPendingIntent(R.id.pbWidMediaNext, WidgetNext);
		    
		    Intent Widget_Prev_intent = new Intent(this, GMService.class);
		    Widget_Prev_intent.setAction(WIDGET_PREV);
		    PendingIntent WidgetPrev = PendingIntent.getBroadcast(this, 0, new Intent(WIDGET_PREV).setComponent(thisWidget), 0);
			updateViews.setonclickPendingIntent(R.id.pbWidMediaPrev, WidgetPrev);
			
            AppWidgetManager manager = AppWidgetManager.getInstance(this);
            manager.updateAppWidget(thisWidget, updateViews);
	}



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


And this is the Intent-filter for the Service
                <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" />


Side-note:
I've also tried to receive it like this
final IntentFilter theFilter = new IntentFilter();
	        theFilter.addAction(Intent.ACTION_HEADSET_PLUG);
	        theFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
	        theFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
	        theFilter.addAction(AppWidgetManager.ACTION_APPWIDGET_ENABLED);
	        theFilter.addAction(WIDGET_PAUSE_PLAY);
	        theFilter.addAction(WIDGET_NEXT);
	        theFilter.addAction(WIDGET_PREV);


	        this.yourReceiver = new BroadcastReceiver() {
	        //	@Override
	            public void onReceive(Context context, Intent intent) {
	            	String action = intent.getAction();
	            	Log.d(TAG, action);
	            	try {
	            	if(action.contains("PHONE_STATE")) {
	            		String s = intent.getStringExtra("state");
	            		if (s.contains("OFF") && mp.isPlaying() && mp != null) {
	            			mp.pause();
	            			WasPlayingBefore = true;
	            		} else if (s.contains("RING") && mp != null && mp.isPlaying()) {
	            			mp.pause();
	            			WasPlayingBefore = true;
	            		} else if (s.contains("IDL") && mp != null) {
	            			if (WasPlayingBefore) {
	            			mp.start();
	            			}
	            			
	            		}
	            		Log.d(TAG, s);
	            	} else if (action.contains(WIDGET_PREV)) {
	            		mClient.OnRequestChangeTrack(-1);
	            	} else if (action.contains(WIDGET_NEXT)) {
	            		mClient.OnRequestChangeTrack(1);
	            	} else if (action.contains(WIDGET_PAUSE_PLAY)) {
	            		if (mp != null) {
	            			try {
	            				if (mp.isPlaying()) {
	            					mp.pause();
	            				} else {
	            					mp.start();
	            				}
	            			} catch (Exception e) {
	            				e.fillInStackTrace();
	            			}
	            		//	SetWidgetListener();
	            		}
	            	} else if (action.contains("android.appwidget.action.APPWIDGET_ENABLED")) {
	            		SetWidgetListener();
	            		if (mp!= null) {
	            			updateWidgetText("","");
	            		} else {
	            			updateWidgetText(" ", " ");
	            		}
	            	}
	            	} catch (Exception e) {
	            		e.fillInStackTrace();
	            	}
	            }
	        };


	        this.registerReceiver(this.yourReceiver, theFilter);



Hope someone can help me.

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Widget OnClickPendingIntent Not Received By Service

#2 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Widget OnClickPendingIntent Not Received By Service

Posted 08 November 2012 - 04:06 PM

Solved it!
Has to use PendingIntent.getService, and to listen to the calls, i couldn't use OnReceive, but i would use onstartCommand()

Hope this can be useful to someone.

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1