In my application I am using a simply way to send broadcast and receiver them.
Intent in = new Intent("UPDATE_SOMETHING");
sendBroadcast(in);
And in my receiver I just check if(intent.getAction().equals(“UPDATE_SOMETHING”)), of course in AndroidManifest in intent-filter i put so that the application know which receiver should receive it. This method works fine for me, and than i find out that this can be done i other way.
Intent in = new Intent(getApplicationContext(), Receiver.class);
in.setAction("UPDATE_SOMETHING");
sendBroadcast(in);
In this example i don’t need to put the action in AndroidManifest, and i can check it in the receiver in the same way as above (if(intent.get…))
Also there is a third way
Intent in = new Intent();
in.setAction("UPDATE_SOMETHING");
sendBroadcast(in);
And puting a action in AndroidManifest…
My question is, which of this three ways is the best to use?, and are there any differences between this ways? (except in writing the code) 🙂
First and third method are the same. The only difference how you pass actions to intent (through constructor or method). Both of these methods will send broadcast to all registered broadcast receivers who listens for UPDATE_SOMETHING action.
In second method, you send broadcast explicitly to your Receiver class. So no other broadcast receiver will see/receive it.