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