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

来源:互联网 发布:中文词频统计软件 编辑:程序博客网 时间:2024/06/07 10:06

项目中要得到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


原创粉丝点击