Android listens for Usb or SDCard hot plugging events

Source: Internet
Author: User

Sometimes we may want to listen for some Usb or SDCard plugging events, which is actually very simple. When we do the plugging, such as Usb or SDCard, the android underlying layer will send the corresponding Intent to the upper layer for use, for example, SDCard or USB android will send such an Intent. ACTION_MEDIA_MOUNTED, so we only need to listen to this Intent in upper-layer applications. Many people on the Internet do not know how to identify whether it is Usb or SDCard. In fact, this Intent contains a Data, the path parameter is encapsulated in Data. We can determine whether the path is Usb or SDCard.
Android underlying frameworks/base/services/java/com/android/server/MountService. java sends the Intent Code as follows:
 
If (newState = VolumeState. Mounted) {www.2cto.com
If (DEBUG_EVENTS) Slog. w (TAG, "updating volume state mounted ");
UpdatePublicVolumeState (path, Environment. MEDIA_MOUNTED );
In = new Intent (Intent. ACTION_MEDIA_MOUNTED, Uri. parse ("file: //" + path ));
In. putExtra ("read-only", false );

Code processed in the application:
 
If (action. equals (Intent. ACTION_MEDIA_MOUNTED )){
String path = intent. getData (). toString ()
. Substring ("file: //". length ());
StartUpGrade (intent. getData ());
}
OK, so that the usb and sdcard events can be distinguished based on the path.

From: anjoy's column

Related Article

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.