我们是以DV6300-T的平台来做测试的,发现有2种方式来检测android中external media(包括SD卡,USB)的状态。
一种是使用StorageListener监听,还有一种是使用广播的方式。
DV6300-T的存储设备相关分析:
相关的类主要有:
RecordDeviceManager
主要采用了观察者模式对设备拔插的监控来触发各种不同情况:
比如在DTVLauncher中就增加了观察者mRecordDeviceListener,在检测到设备拔出时候会停止时移或录制等。
第一种监测方式:
使用StorageManager
StorageManager mStorageManager = (StorageManager)context.getSystemServic(Context.STORAGE_SERVICE);
mStorageManager.registerListener(mStorageListener);
IMountService mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
StorageEventListener mStorageListener = new StorageEventListener() {
};
我们可以根据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{
}
通过广播传递过来的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 框架层为IMountService 增加新接口
android usb挂载分析---MountService启动
Android2.2 Vold 分析-(四)---Vold 消息接收及挂载/卸载处理部分分析
Android2.2 Vold 分析(三)---Vold 中 volumeManager分析
Android2.2 Vold 分析-(二)---Vold 中 Netlink事件通信机制分析
Android Vold 分析(一)--system/vold/main.cpp-----mian函数分析
android usb流程(转载加整理)
http://blog.163.com/lzh_327/blog/static/7219480201122103947556/
【转】android vold浅析(2)
Android SDCard UnMounted 流程分析(一)
Android SDCard UnMounted 流程分析(二)
Android SDCard UnMounted 流程分析(三)
android usb挂载分析---MountService启动
android usb挂载分析----vold启动