// Listener
Private Final broadcastreceiver sdcardlistener = new broadcastreceiver (){
@ Override
Public void onreceive (context, intent ){
String action = intent. getaction ();
Log. D ("tag", "sdcard Action:" + action );
If (intent. action_media_mounted.equals (Action)
| Intent. action_media_scanner_started.equals (Action)
| Intent. action_media_scanner_finished.equals (Action)
){
// The SD card is mounted successfully.
} Else if (intent. action_media_removed.equals (Action)
| Intent. action_media_unmounted.equals (Action)
| Intent. action_media_bad_removal.equals (Action)
){
// An error occurred while mounting the SD card.
}
}
};
// Register the listener
Private void registersdcardlistener (){
Intentfilter = new intentfilter (intent. action_media_mounted );
Intentfilter. addaction (intent. action_media_scanner_started );
Intentfilter. addaction (intent. action_media_scanner_finished );
Intentfilter. addaction (intent. action_media_removed );
Intentfilter. addaction (intent. action_media_unmounted );
Intentfilter. addaction (intent. action_media_bad_removal );
Intentfilter. adddatascheme ("file ");
Registerreceiver (sdcardlistener, intentfilter );
}
Log out of the listener after use:
unregisterReceiver(sdcardListener);