Sudarshan sridhar
Sudarshan sridhar

Reputation: 201

updating Textview of a widget when a service that runs a music player actually started playing

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 ?

Upvotes: 0

Views: 220

Answers (1)

Eldhose M Babu
Eldhose M Babu

Reputation: 14520

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

Upvotes: 1

Related Questions