Reputation: 10830
I have been able to get my BroadcastReceiver running with this:
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="false">
</receiver>
As you can see no <intent-filter>
. It gets called correctly with the right Intent
extras. But I have been looking around and I am confused as to whether I need one or not? I do have a setAction()
method called on my Intent
s but to make them unique from others to ensure a specific issue with notifications, not actually using that action's string. But what exactly is the correlation? Thanks in advance.
Intent intent = new Intent(this.getContext(), AlarmReceiver.class);
intent.setAction("com.something"+System.currentTimeMillis());
//... extras are here
PendingIntent pi = PendingIntent.getBroadcast(this.getContext(), 123, intent, PendingIntent.FLAG_CANCEL_CURRENT|Intent.FILL_IN_DATA);
AlarmManager alarm = (AlarmManager)getContext().getSystemService(Activity.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pi);
Using that with what I have in the manifest file works.
EDIT I found this on the Android Developers blog, and it states this:
Implicit Intents only specify “what” they should match, using actions, categories, data, MIME types, and so on. The exact components that they will find are only determined at run-time, by the Package Manager matching it against the current applications.
Explicit Intents specify a single explicit “who” they should match, through a ComponentName. Regardless of whatever else is in the Intent, it is only associated with the exact manifest package name and class name as given in its ComponentName.
I am still slightly confused with this explanation, but it seems to be the closest thing to what I should be doing. So I am sure I am using an implicit intent. Question is, is it ok that I am leaving out <intent-filter>
? I am matching it to a specific class. There may not be an actual action tying them together, perse, but is the class enough?
Upvotes: 3
Views: 2193
Reputation: 4284
<intent-filter>
is required when you want to start your receiver using implicit intents, if you are always using explicit intents to start your broadcast receiver then intent-filters are not required.
see this SO Post.
Upvotes: 4
Reputation: 13541
The entire way a broadcast receiver works is it involves using an Intent Filter to catch specific broadcast intents that are sent within the system. So if you give the receiver no intent filter, then NOTHING will be sent to it and its code will never execute.
Please read the documentation of Intent and learn how Intent allows IPC.
Update
Also its possible that without any declarations it could be receiving all public broadcasts and the way your code handles it.. is it ignores all the broadcasts except the ones it cares about. I've never done that this kind of implementation because I follow the standard. Which is to declare either as a dynamic listener (Programatic) or a static listener (XML declaration), where the dynamic listener would have its Intent Filter set in code.
Upvotes: -1