Android USB flash drive detection and Memory Retrieval

Source: Internet
Author: User

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

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.