Broadcast Receiver
(Android phone – how-to/example)
Android implements in an elegant manner the intent broadcast. One will use this, if for example you want to register to some actions (ex: one wants to know when something happens to the sdcard).
For the sdcard example, in order to register your “intentions”, use the following:
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
intentFilter.addDataScheme(”file”);
registerReceiver(m_brSDcardEvent, intentFilter);
where m_brSDcardEvent is the Broadcast Receiver itself, while the intentFilter is the filter where we register our interests.
To receive the intents and using them, is done in the Broadcast Receiver:
private final BroadcastReceiver m_brSDcardEvent = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED))
{
// SD card available
}
else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)
|| action.equals(Intent.ACTION_MEDIA_CHECKING))
{
….
all other intents will be treated similarly.
Note: do not forget to un-register your activity from the broadcast, if you do not want to alter the functionality of all other applications residing on the phone.
unregisterReceiver(m_brSDcardEvent);












