Send a public broadcast from an AppWidget in Android
- by greve
I have a widget for a music player and want to be able to send broadcasts when pushing the different buttons. What I want to do is when a button is pushed, the widget sends a public broadcast to another BroadcastReceiver so it can handle the different actions.
In my activity class with the BroadcastReceiver:
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Constants.ACTION_NEXT)) {
Log.d("RECEIVER", "ACTION_NEXT");
}
}
};
@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter();
//Widget actions
filter.addAction(Constants.ACTION_NEXT);
registerReceiver(broadcastReceiver, new IntentFilter(filter));
}
In my Widget:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(Constants.ACTION_NEXT), 0);
views.setOnClickPendingIntent(R.id.WidgetNextButton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
Any help or other solutions are very much appreciated!