Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid widget: how to set listener on buttons on widget?
    primarykey
    data
    text
    <p>I have searched between many questions others asked about this problem and all were confusing and not helpful.</p> <p>My problem is I make widget for app, and it starts ok, but it doesn't react on events. I have 2 buttons on widget. Here is the source for class implementing it. </p> <pre><code>public class MediaPlayerWidget extends AppWidgetProvider { public static final String NEXT_SONG = "1"; public static final String PLAY_PAUSE = "2"; private MediaPlayerActivity act=new MediaPlayerActivity(); private void addEvents(Context ctxt) { Intent intent = new Intent(ctxt, MediaPlayerWidget.class); intent.setAction(PLAY_PAUSE); PendingIntent pi = PendingIntent.getBroadcast(ctxt, 0, intent, 0); RemoteViews views = new RemoteViews(ctxt.getPackageName(), R.layout.widget_layout); views.setOnClickPendingIntent(R.id.widget_play, pi); intent=new Intent(ctxt,MediaPlayerWidget.class); intent.setAction(NEXT_SONG); pi=PendingIntent.getBroadcast(ctxt, 0, intent, 0); views.setOnClickPendingIntent(R.id.widget_next, pi); } @Override public void onReceive(Context ctxt,Intent i){ String cmp=i.getAction(); if(cmp.equals(NEXT_SONG)){ act.nextSong(); }else if(cmp.equals(PLAY_PAUSE)){ act.playPause(); } } @Override public void onUpdate(Context ctxt, AppWidgetManager mgr, int[] appWidgetIds) { addEvents(ctxt); } } </code></pre> <p>Does anyone know what seems to be the problem? I have seen some other solutions with Service doing refresh but since I have 2 buttons I don't need such thing. If any other code is needed I will supply it.</p> <p>EDIT:</p> <p>If you don't know what's wrong with code above, can anyone tell me or give some sample code on how to implement button listener/event in widget.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload