Widget OnClick self firing

Put your problem here if it does not fit any of the other categories.

Widget OnClick self firing

Postby EndLessMind » Wed Oct 10, 2012 4:29 am

I have a service the controls a mediaplayer and updates a widget. I added 3 ImageViews to change song (Prev and Next) and to Play/Pause. I added custom BroadcastReceivers, one for each ImageView. But when i start the app, no mater of the Widget is one the homescreen or not, the Receiver get broadcasts from widget.

Code: Select all
public void SetWidgetListener() {
     RemoteViews updateViews = null;
        updateViews = new RemoteViews(this.getPackageName(), R.layout.widget);
        Intent intent = new Intent(this, GMWidget.class);
        intent.setAction(WIDGET_PAUSE_PLAY);
        PendingIntent WidgetPausePlay = PendingIntent.getBroadcast(this, 0, intent, 0);
        updateViews.setOnClickPendingIntent(R.id.pbWidMediaPlay, WidgetPausePlay);

        Intent Widget_Next_intent = new Intent(this, GMWidget.class);
        Widget_Next_intent.setAction(WIDGET_NEXT);
        PendingIntent WidgetNext = PendingIntent.getBroadcast(this, 0, Widget_Next_intent, 0);
        updateViews.setOnClickPendingIntent(R.id.pbWidMediaNext, WidgetNext);

        Intent Widget_Prev_intent = new Intent(this, GMWidget.class);
        Widget_Prev_intent.setAction(WIDGET_PREV);
        PendingIntent WidgetPrev = PendingIntent.getBroadcast(this, 0, Widget_Prev_intent, 0);
        updateViews.setOnClickPendingIntent(R.id.pbWidMediaPrev, WidgetPrev);

        ComponentName thisWidget = new ComponentName(this, GMWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);
}


And this is where i create my Receiver:
Code: Select all
public void onCreate() {
        super.onCreate();

        try {
        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("com.groove.mobile.WIDGET_NEXT");
        theFilter.addAction("com.groove.mobile.WIDGET_PREV");
        theFilter.addAction("com.groove.mobile.WIDGET_PAUSE_PLAY");

        this.yourReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                Log.d(TAG, action);

                if(action.contains("PHONE_STATE")) {
                    String s = intent.getStringExtra("state");
                    if (s.contains("OFF") && mp.isPlaying()) {
                        if (mp != null) {
                        mp.pause();}
                    } else if (s.contains("RING")) {
                        if (mp != null && mp.isPlaying()) {
                        mp.pause();}
                    } else if (s.contains("IDL")) {
                        if (mp != null) {
                        mp.start();}
                    }
                    Log.d(TAG, s);
                    //mp.pause();
                } else if (action.contains(WIDGET_PREV)) {
                    mClient.OnRequestChangeTrack(-1);
                //  SetWidgetListener();
                } else if (action.contains(WIDGET_NEXT)) {
                    mClient.OnRequestChangeTrack(1);
                //  SetWidgetListener();
                } 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(" ", " ");
                    }
                }
            }
        };


        this.registerReceiver(this.yourReceiver, theFilter);
        } catch (Exception e) {
            e.fillInStackTrace();
        }
    }



Here is the code for my widget

Code: Select all
public class GMWidget extends AppWidgetProvider {
    private String TAG = "GMWidget";


    @Override
    public void onEnabled(Context context) {
    }

    String LOG = "GMWidget";
    @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager,
          int[] appWidgetIds) {
        Log.w(LOG, "onUpdate method called");



    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        String action = intent.getAction();
        //Log.d(TAG,"CLICKK: " + action);
        Intent i = new Intent();
        i.setAction(action);
        context.sendBroadcast(i);

    }


}


The reason to why i re-broadcast everything from the widget, is that my service won't receive my custom broadcast otherwise.

This is my service and widget code from the manifest:
Code: Select all
  <service android:icon="@drawable/ic_launcher"
       android:label="@string/app_name" android:permission="android.permission.INTERNET" android:name="com.groove.services.GMService">
                                    <intent-filter>
                <action android:name="android.media.AUDIO_BECOMING_NOISY" />
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
               
                <action android:name="com.groove.mobile.WIDGET_NEXT" />
                <action android:name="com.groove.mobile.WIDGET_PREV" />
                <action android:name="com.groove.mobile.WIDGET_PAUSE_PLAY" />
               
                <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
            </intent-filter>
                 
             </service>
<receiver android:name=".specials.GMWidget" android:enabled="false">
             <intent-filter>
                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                  <action android:name="com.groove.mobile.WIDGET_NEXT" />
                <action android:name="com.groove.mobile.WIDGET_PREV" />
                <action android:name="com.groove.mobile.WIDGET_PAUSE_PLAY" />
            </intent-filter>

       
       <meta-data
          android:name="android.appwidget.provider"
          android:resource="@xml/widget_info" />
             </receiver>

Thanks in advance!
EndLessMind
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 10, 2012 4:14 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 12 guests