当前位置:操作系统 > 安卓/Android >>

android U盘检测及获取内存信息

在网上查了一些资料。U盘检测的方法一般有两种方式,检测电池或者查看配置信息。

1.Batterymanager检测方法如下:


[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)得到的值分别为:

0):断开

1):连上USB

2):连上了充电器

 

2检测listroot是否有文件路径多出


[java]
public class testU implements Runnable{ 
   private File[] roots=File.listRoots(); 
   public testU() { 
    } 
    
   public void run() { 
       System.out.println("检测系统开启..."); 
       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("插入盘符:"+tempFile[i].toString()); 
                    } 
                } 
                roots=File.listRoots();//更新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("退出盘符:"+roots[i].toString()); 
                    } 
                } 
                roots=File.listRoots();//更新roots  
           } 
           try { 
                Thread.sleep(1000); 
           } catch (InterruptedExceptio

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,