android外接存储设备状态判断-USB和SD卡连接状态

项目中要得到USB的连接状态,在3.1以前的版本中,有一个ACTION_UMS_CONNECTED广播,表示手机已进入大容量模式,但是没有消息说USB有没有连上 。3.1以上的版本包含了一个android.hardware.usb ,也没有相应的API。其实我们可以从batterymanager入手:

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:连上了充电器

----------------------------------------------------------------------------------------------------------------------------------------------


private final BroadcastReceiver broadcastRec = newBroadcastReceiver(){
     public void onReceive(Context context, Intent intent){
      if(intent.getAction().equals("android.intent.action.MEDIA_MOUNTED"))
      {  
        File file=newFile("/sdcard/card/");
        File file2=newFile("/usb/");
       if(file.exists() && !isSdcard) { //判断SD卡 插入 
        isSdcard=true;
          Log.d("explorer","SDcard is useful!");
          Toast.makeText(context, R.string.SDCardin, Toast.LENGTH_LONG).show();
          btn_sdcard.setEnabled(true);
           
           String strPath="/sdcard/card/";
            
           path_list.AddPathToList(strPath);
           updateFileList();
          }
       elseif(file2.exists() && !isUsb) {  //判断usb 接入 
          //
           isUsb=true;
           Log.d("explorer","USB is useful!");
           Toast.makeText(context, R.string.USBin, Toast.LENGTH_LONG).show();
           btn_usb.setEnabled(true);
            
           String strPath="/usb/";
            
            path_list.AddPathToList(strPath);//显示当前路径为usb
            updateFileList();//更新文件显示列表
          }
        
        
      }elseif(intent.getAction().equals("android.intent.action.MEDIA_REMOVED")
        ||intent.getAction().equals("android.intent.action.MEDIA_UNMOUNTED")
        ||intent.getAction().equals("android.intent.action.MEDIA_BAD_REMOVAL")){
               
       File file=newFile("/sdcard/card/");
       File file2=newFile("/usb/");
       if(!file.exists() && isSdcard) {   
          Log.d("explorer","SDcard is not useful!");
          Toast.makeText(context, R.string.SDCardout, Toast.LENGTH_LONG).show();
          isSdcard=false;
          btn_sdcard.setEnabled(false);
           if(path_list.GetCurPath().startsWith("/sdcard/")){
         select_list.ClearList();
         path_list.AddPathToList("/");
        }
            
          }
               
       elseif(!file2.exists() && isUsb){   
            
        isUsb=false;
           Log.d("explorer","USB is not useful!");
           Toast.makeText(context, R.string.USBout, Toast.LENGTH_LONG).show();
           btn_usb.setEnabled(false);
            
           if(path_list.GetCurPath().startsWith("/usb/")){
         select_list.ClearList();
         path_list.AddPathToList("/");
        }
          }
        
       updateFileList();
      }
     }
   };

以上是广播接收外接存储插拔状态的。还有就是程序启动时判断usb和sdcard是否存在的:

private void initList(){
      
      File file=newFile("/sdcard/card/");
         if(file.exists()) {   
          Log.d("explorer","SDcard is useful!");
          isSdcard=true;
          btn_sdcard.setEnabled(true);           
          }else{   
          // 当前不可用 
           isSdcard=false;
           Log.d("explorer","SDcard is no use!");
           btn_sdcard.setEnabled(false);
          }
         String strPath="/";
         File file2=newFile("/usb/");
         if(file2.exists()) {
           
          isUsb=true;
          btn_usb.setEnabled(true);
           if(isSdcard) {
              strPath="/sdcard/card/";                     
     }else{
       strPath="/usb/";                
     }
           
   }else{
   isUsb=false;
   btn_usb.setEnabled(false);
     if(isSdcard) {
       strPath="/sdcard/card/";          
                
     }else{
       strPath="/";        
     }
   }
         path_list.AddPathToList(strPath);
         updateFileList();
   }


最近总有人问能否同时识别多个存储设备,我提供个思路,没有试验过:
     在android广播插入存储设备时,它的intent中有data,用方法getDataString()获取其中数据你会发现那里面是这个设备的路径,你根据自己的需要截取路径,并根据这个来识别不同的存储设备。

String path=intent.getDataString();

path=path.substring(11)+"/";//path=file:///mnt/sdcard/external_sdcard


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值