Search code examples
androidandroid-intentandroid-pendingintentandroid-broadcastandroid-broadcastreceiver

android - set both class and action for intent


I have an Intent that I initiate like this:

notificationIntent = new Intent(context, HomeActivity.class);

This intent is attached to an ongoing notification.

Now, in addition to the class opened when clicking the intent, I want to add an action string to the intent, so that when the notification clicked my custom BroadcastReceiver that listens to the same action string will trigger.

notificationIntent.setAction(context.getString(R.string.notification_clicked_action_string));

Problem is, for some reason, the BroadcastReceiver is not called, and I have other BroadcastReceiver that I registered problematically like this and they work fine.

So, it is a problem to have both a class and an action in an intent?


Solution

  • So, it is a problem to have both a class and an action in an intent?

    No, that is perfectly fine.

    However, unless you have a very strange naming system, HomeActivity is an activity. That means that new Intent(context, HomeActivity.class) identifies that activity, and you are hopefully using that with PendingIntent.getActivity(). You cannot have a BroadcastReceiver respond to startActivity(), which is what will be called when the PendingIntent is invoked.