0

I have written a music player service (MusicPlayer) that takes 2 inputs 1) play list names and 2) path to each song/audio.

This is working perfectly.

I have a widget for the app that show an image currently. I am planning to add a textview so that current playing song will be shown and will updated whenever a new song being played.

Unable to figure out how to update widget from a service.

is there a way I can accesses widget to update required information ?

  • Check this: [update textview title in an Activity from a service](https://stackoverflow.com/a/30996212/5343320) – huk Jul 12 '17 at 18:23

1 Answers1

1

Sure, You can do that.

The widgetProvider is a broadcast receiver itself. So inorder to communicate with a widget, you just need to send a broadcast.

Intent intent = new Intent(this,YourWidgetProvider.class);
intent.putExtra("SONG","Your Song");
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
// Use an array and EXTRA_APPWIDGET_IDS instead of 
AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int[] ids = {widgetId};
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
sendBroadcast(intent);

In onReceive Method of widgetprovider, You can extract the info required from the intent

Reference : Programmatically update widget from activity/service/receiver

Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44