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 框架層為IMountService 增加新接口