Android monitors the status of external storage devices (SD card, USB flash drive, etc.)

Source: Internet
Author: User

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.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.