Android存储设备(U盘,SD卡)状态监测

我们是以DV6300-T的平台来做测试的,发现有2种方式来检测android中external media(包括SD卡,USB)的状态。

一种是使用StorageListener监听,还有一种是使用广播的方式。

 

DV6300-T的存储设备相关分析:

相关的类主要有:
RecordDeviceManager   DeviceStateListener  ChoiceRecordDevice
主要采用了观察者模式对设备拔插的监控来触发各种不同情况:

比如在DTVLauncher中就增加了观察者mRecordDeviceListener,在检测到设备拔出时候会停止时移或录制等。

 

第一种监测方式:

使用StorageManager  IMountService  StorageEventListener等类来控制(可以参考DV6300-T的源码):

 

StorageManager mStorageManager = (StorageManager)context.getSystemServic(Context.STORAGE_SERVICE);
mStorageManager.registerListener(mStorageListener);
IMountService mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));

StorageEventListener mStorageListener = new StorageEventListener() {
    @Override
    public void onStorageStateChanged(String path, String oldState,String newState) {
        if(path.equals(mRecordDeviceStorageName)) {

        Log.i("usb",path+":"+oldState+"--->"+"newState");          

           
            if(newState.equals(Environment.MEDIA_UNMOUNTED)) {
                notifyObservers();
            }
        }
  }
};

我们可以根据onStorageStateChanged方法中的3个参数来判断当前的状态,根据path路径来判断是SD卡(/mnt/sdcard)还是USB设备(/mnt/sda)。

比如在DV6300-T上,我们打印如下:

插SD卡:
会调用3次onStorageStateChanged:参数分别是:
/mnt/sdcard/extend_sd : removed--->unmounted
/mnt/sdcard/extend_sd : unmounted--->checking
/mnt/sdcard/extend_sd : checking--->mounted

插U盘:
/mnt/sda1 :unmounted--->checking
/mnt/sda1 :checking--->mounted
拔SD卡:
/mnt/sdcard/extend_sd : mounted--->unmounted
/mnt/sdcard/extend_sd : unmounted--->removed

拔U盘:
/mnt/sda1 :mounted--->unmounted
/mnt/sda1 :unmounted--->removed
/mnt/sda1 :removed--->unmounted

 

 

第2种监测方式(广播方式):

class UsbReceiver{
   private BroadcastReceiver mReceiver;
   UsbReceiver(Context context){
      mReceiver = new BroadcastReceiver(){
      @Override
      public void onReceive(Context context, Intent intent) {

         //intent.getAction());获取存储设备当前状态        

         Log.i("usb","BroadcastReceiver:"+intent.getAction());

         //intent.getData().getPath());获取存储设备路径
         Log.i("usb","path:"+intent.getData().getPath());

        }

     };
  
      IntentFilter filter = new IntentFilter();
      filter.addAction(Intent.ACTION_MEDIA_SHARED);//如果SDCard未安装,并通过USB大容量存储共享返回
      filter.addAction(Intent.ACTION_MEDIA_MOUNTED);//表明sd对象是存在并具有读/写权限
      filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);//SDCard已卸掉,如果SDCard是存在但没有被安装
      filter.addAction(Intent.ACTION_MEDIA_CHECKING);  //表明对象正在磁盘检查
      filter.addAction(Intent.ACTION_MEDIA_EJECT);  //物理的拔出 SDCARD
      filter.addAction(Intent.ACTION_MEDIA_REMOVED);  //完全拔出
      filter.addDataScheme("file"); // 必须要有此行,否则无法收到广播   
      context.registerReceiver(mReceiver, filter);
 }
}

通过广播传递过来的intent.getData()会得到一个uri,然后uri.getPath()就是插上usb的路径,可以记录下每次插上或者拔出的usb的路径,
比如我们在DV6300平台上:
U盘就返回/mnt/sda1,而SD卡返回/mnt/sdcard/extend_sd
而getAction会获取当前状态,如下描述:

U盘插入:
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_CHECKING
intent.getAction() == android.intent.action.MEDIA_MOUNTED
U盘拔出:
intent.getAction() == android.intent.action.MEDIA_EJECT
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_REMOVED
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED

SD卡插入:
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_CHECKING
intent.getAction() == android.intent.action.MEDIA_MOUNTED
SD卡拔出:
intent.getAction() == android.intent.action.MEDIA_EJECT
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_REMOVED

 

 

 

参考博文:

android 监控usb插拔

Android 框架层为IMountService 增加新接口

android usb挂载分析---MountService启动

Android深入浅出之Binder机制

Android 2.3 SD卡挂载流程浅析(七)


 

你可能感兴趣的:(Android存储设备(U盘,SD卡)状态监测)