7

I'm working on a music player-widget (home screen widget).. It only needs to play 1 song, (Preferably with the MediaPlayer class). But I'm not sure how to implement it. I'm kind of inexperienced with Android development, just so that's mentioned.

The class I have so far extends AppWidgetProvider, and I guess it is not a good idea to let this class handle the music-playing part, but rather a Service. And if so, how?

Furthermore, I have 3 buttons: play, pause and stop, and I can distinguish which one has been pressed in onReceive(...).

Thanks in advance!


Here is the class.

public class MusicManager extends AppWidgetProvider {

    private final String ACTION_WIDGET_PLAY = "PlaySong";
    private final String ACTION_WIDGET_PAUSE = "PauseSong";
    private final String ACTION_WIDGET_STOP = "StopSong";   
    private final int INTENT_FLAGS = 0;
    private final int REQUEST_CODE = 0;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        RemoteViews controlButtons = new RemoteViews(context.getPackageName(),
                R.layout.main);

        Intent playIntent = new Intent(context, MusicService.class);

        Intent pauseIntent = new Intent(context, MusicService.class);

        Intent stopIntent = new Intent(context, MusicService.class);


        PendingIntent playPendingIntent = PendingIntent.getService(
                context, REQUEST_CODE, playIntent, INTENT_FLAGS);
        PendingIntent pausePendingIntent = PendingIntent.getService(
                context, REQUEST_CODE, pauseIntent, INTENT_FLAGS);
        PendingIntent stopPendingIntent = PendingIntent.getService(
                context, REQUEST_CODE, stopIntent, INTENT_FLAGS);

        controlButtons.setOnClickPendingIntent(
                R.id.btnPlay, playPendingIntent);
        controlButtons.setOnClickPendingIntent(
                R.id.btnPause, pausePendingIntent);
        controlButtons.setOnClickPendingIntent(
                R.id.btnStop, stopPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);         
    }
}
Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
whirlwin
  • 16,044
  • 17
  • 67
  • 98

2 Answers2

4

Added <service android:name=".MusicService" android:enabled="true" /> to the manifest!

whirlwin
  • 16,044
  • 17
  • 67
  • 98
  • Me too having same requirement as yours. Can you please post your service class over here ? – AndyN Apr 01 '14 at 06:55
  • @KeTaN I wish I could, but I deleted this code many years ago. Sorry! Hope you find an answer somewhere else. – whirlwin Apr 01 '14 at 07:27
  • @whirlwin Still can you tell me that what kind of code we can written in our service according to your MusicService class if you have remembered?? – KeTaN Apr 01 '14 at 07:30
0

In the onUpdate(...) method of your AppWidgetProvider, use something like this to start a service (this example associates the service to a button click event):

Intent intent = new Intent(context, MusicService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);

For more info look here

aromero
  • 25,681
  • 6
  • 57
  • 79
  • Thanks for the answer, but it still doesn't work. I'm probably missing something really obvious here. – whirlwin Nov 28 '10 at 22:22
  • How do you know the problem isn't in the services? Make the services log something when they start so you know at least if they are being called. – aromero Nov 29 '10 at 00:13
  • I did manual debugging with Toast, but the problem was that I forgot to include the service in the manifest! I hate myself because of that right now.. – whirlwin Nov 29 '10 at 00:22