开发者

Send a public broadcast from an AppWidget in Android

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 开发者_如何学Govoid 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!

EDIT: Forgot to mention my actual problem: The BroadcastReceiver never receive the broadcast

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜