Send a public broadcast from an AppWidget in Android

Posted by greve on Stack Overflow See other posts from Stack Overflow or by greve
Published on 2010-06-15T10:19:53Z Indexed on 2010/06/15 10:22 UTC
Read the original article Hit count: 415

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!

© Stack Overflow or respective owner

Related posts about android

Related posts about widget