I checked some information online. There are two methods for detecting a USB flash disk: checking the battery or checking the configuration information.
1. The Batterymanager detection method is as follows:
[Java]
IntentFilter mIntentFilter = new IntentFilter ();
MIntentFilter. addAction (Intent. ACTION_BATTERY_CHANGED );
RegisterReceiver (BtStatusReceiver, mIntentFilter );
Public BroadcastReceiver BtStatusReceiver = new BroadcastReceiver () // receive broadcast that BT Adapter status change
{
@ Override
Public void onReceive (Context context, Intent intent)
{
String action = intent. getAction ();
If (action. equals (Intent. ACTION_BATTERY_CHANGED ))
{
Log. d ("Battery", "" + intent. getIntExtra ("plugged", 0 ));
Toast text = Toast. makeText (context, "ACTION_USB_DEVICE_ATTACHED" + intent. getIntExtra ("plugged", 0), Toast. LENGTH_LONG );
Text. show ();
Dataview. setText ("" + intent. getIntExtra ("plugged", 0 ));
}
}
};
IntentFilter mIntentFilter = new IntentFilter ();
MIntentFilter. addAction (Intent. ACTION_BATTERY_CHANGED );
RegisterReceiver (BtStatusReceiver, mIntentFilter );
Public BroadcastReceiver BtStatusReceiver = new BroadcastReceiver () // receive broadcast that BT Adapter status change
{
@ Override
Public void onReceive (Context context, Intent intent)
{
String action = intent. getAction ();
If (action. equals (Intent. ACTION_BATTERY_CHANGED ))
{
Log. d ("Battery", "" + intent. getIntExtra ("plugged", 0 ));
Toast text = Toast. makeText (context, "ACTION_USB_DEVICE_ATTACHED" + intent. getIntExtra ("plugged", 0), Toast. LENGTH_LONG );
Text. show ();
Dataview. setText ("" + intent. getIntExtra ("plugged", 0 ));
}
}
};
Intent. getIntExtra ("plugged", 0) obtain the following values:
0): disconnected
1): connect to USB
2): connected to the charger
2. check whether there are more file paths in listroot.
[Java]
Public class testU implements Runnable {
Private File [] roots = File. listRoots ();
Public testU (){
}
Public void run (){
System. out. println ("detection System enabled ...");
While (true ){
File [] tempFile = File. listRoots ();
Boolean sign = false;
If (tempFile. length> roots. length ){
For (int I = tempFile. length-1; I> = 0; I --){
Sign = false;
For (int j = roots. length-1; j> = 0; j --){
If (tempFile [I]. equals (roots [j]) {
Sign = true;
}
}
If (sign = false ){
System. out. println ("insert drive letter:" + tempFile [I]. toString ());
}
}
Roots = File. listRoots (); // update the roots
} Else {
For (int I = roots. length-1; I> = 0; I --){
Sign = false;
For (int j = tempFile. length-1; j> = 0; j --){
If (tempFile [j]. equals (roots [I]) {
Sign = true;
}
}
If (sign = false ){
System. out. println ("Exit letter:" + roots [I]. toString ());
}
}
Roots = File. listRoots (); // update the roots
}
Try {
Thread. sleep (1000 );
} Catch (InterruptedException ex ){
Logger. getLogger (testU. class. getName (). log (Level. SEVERE, null, ex );
}
}
}
Public static void main (String args []) {
New Thread (new testU (). start ();
}
}
Public class testU implements Runnable {
Private File [] roots = File. listRoots ();
Public testU (){
}
Public void run (){
System. out. println ("detection System enabled ...");
While (true ){
File [] tempFile = File. listRoots ();
Boolean sign = false;
If (tempFile. length> roots. length ){
For (int I = tempFile. length-1; I> = 0; I --){
Sign = false;
For (int j = roots. length-1; j> = 0; j --){
If (tempFile [I]. equals (roots [j]) {
Sign = true;
}
}
If (sign = false ){
System. out. println ("insert drive letter:" + tempFile [I]. toString ());
}
}
Roots = File. listRoots (); // update the roots
} Else {
For (int I = roots. length-1; I> = 0; I --){
Sign = false;
For (int j = tempFile. length-1; j> = 0; j --){
If (tempFile [j]. equals (roots [I]) {
Sign = true;
}
}
If (sign = false ){
System. out. println ("Exit letter:" + roots [I]. toString ());
}
}
Roots = File. listRoots (); // update the roots
}
Try {
Thread. sleep (1000 );
} Catch (InterruptedException ex ){
Logger. getLogger (testU. class. getName (). log (Level. SEVERE, null, ex );
}
}
}
Public static void main (String args []) {
New Thread (new testU (). start ();
}
}
After detecting the insertion of a USB flash drive, you can use statFs to obtain the total memory and available memory of the USB flash drive. Similar to SD card.
Final StatFs stat = new StatFs (Udisk. getPath ());
Final long blockSize = stat. getBlockSize ();
Final long totalBlocks = stat. getBlockCount ();
Final long availableBlocks = stat. getAvailableBlocks ();
Long mTotalSize = totalBlocks * blockSize;
Long mAvailSize = availableBlocks * blockSize;
Author: abc137508277