您的位置:首页 > 移动开发 > Android开发

Android存储设备(U盘,SD卡)状态监测(《Android 2.3 SD卡挂载流程浅析1234567)

2012-06-21 17:16 567 查看
我们是以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 2.3 SD卡挂载流程浅析(一)》

《Android 2.3 SD卡挂载流程浅析(二)》

《Android 2.3 SD卡挂载流程浅析(三)》

《Android 2.3 SD卡挂载流程浅析(四)》

《Android 2.3 SD卡挂载流程浅析(五)》

《Android 2.3 SD卡挂载流程浅析(六)》

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)

http://hi.baidu.com/nnqidhbd/item/fad934265ed321454699628b

Android SDCard UnMounted 流程分析(一)

Android SDCard
UnMounted 流程分析(二)

Android SDCard UnMounted 流程分析(三)

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

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: