Recently, the status of external storage devices needs to be monitored in the project, so this note has been collated for later viewing.
Broadcasts emitted when the state of an external storage device changes
Compare broadcasts in different states
1. When inserting an external SD card:
2. When removing an external SD card:
3. When connecting the PC into USB mass storage mode:
4. When connecting the PC out of USB mass storage mode:
Code Implementation Monitoring
public void Startlisten () {Intentfilter intentfilter = new Intentfilter (intent.action_media_mounted); Intentfilter.setpriority (1000); Intentfilter.addaction (intent.action_media_unmounted); Intentfilter.addaction (intent.action_media_removed); Intentfilter.addaction (intent.action_media_shared); Intentfilter.addaction (Intent.action_media_bad_removal); 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.addaction (INTENT.ACTION_MEDIA_NOFS); Intentfilter.addaction (Intent.action_media_button); Intentfilter.addaction (Intent.action_media_scanner_scan_file); Intentfilter.adddatascheme ("file"); Registerreceiver (Broadcastrec, intentfilter);} Private final Broadcastreceiver Broadcastrec = new Broadcastreceiver () {@Override public void onreceive (Context Context, Intent Intent) {String action = intent.getaction (); LOG.D ("mediaaction", action); if (Action.equals ("Android.intent.action.MEDIA_MOUNTED")) {//todo} else if (Action.equals (intent. action_media_unmounted) {//todo}else if (action.equals (intent.action_media_scanner_started)) { }else if (action.equals (intent.action_media_scanner_finished)) {}else if (action.equals (intent.action_media_s hared)) {}else {}}};
Android monitors the status of external storage devices (SD card, USB flash drive, etc.)