1、檢測USB或SD卡的插入與拔出
根據Android文檔介紹, 有兩種方法檢測USB設備插入
第一種, 在Manifest 裏面加入
<manifest ...> <uses-feature android:name="android.hardware.usb.host" /> <uses-sdk android:minSdkVersion="12" /> ... <application> <activity ...> ... <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /> </activity> </application> </manifest>
對應的device_filter.mxl
<?xml version="1.0" encoding="utf-8"?> <resources> <usb-device vendor-id="1234" product-id="5678" /> </resources>
其中, vendor-id 和 product-id 每個不同型號的手柄得到的值都是不一樣的.具體獲取方法下面會提到.
做好以上配置以後, 當設備插入的時候, 系統會彈出一個提示框, 詢問你是否要打開這個程序.點擊確定的話就會啓動你的APP.
第二種方法. 遍歷USB Device
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); ... HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); while(deviceIterator.hasNext()){ UsbDevice device = deviceIterator.next() int vendorId = device.getVendorId(); int productIt = device.getProductId(); 如果是用第一種方法檢測的話device_filter.mxl裏面的兩個值要填這兩個 }
用這種遍歷方法我是程序啓動的時候起一條線程隔三秒檢測一次, 檢測到手柄後根據各自需求處理.
參考:http://blog.csdn.net/s278777851/article/details/6956226
2、USB或SD卡拔插廣播檢測:
<intent-filter android:priority="1000">
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file"/>
</intent-filter>
</receiver>
Java:
public class PeripheralDevReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(action.equalsIgnoreCase((Intent.ACTION_MEDIA_MOUNTED))){
String
path = intent.getDataString().substring("file://".length());//獲得SD卡或USB的路徑。
Log.i("PeripheralDevReceiver", "onReceive path_______"+path);
}
...
}
}
此廣播可能是SD卡或是USB拔插觸發而發的。
我用比較愚笨的方法
if(path.contains("usb")){
TestMemorySpeed.usbPath = path;
}
即收到的path含有”usb“即是USB的路徑。可能會出錯。
3、上面是全局接收廣播的,也可以”局部“接收註冊
Java:
註冊:
IntentFilter iFilter = new IntentFilter();
iFilter.addAction(Intent.ACTION_MEDIA_EJECT);
iFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
iFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
iFilter.addDataScheme("file");
iFilter.setPriority(1000);
registerReceiver(receiverSD , iFilter);
receiverSD = new sdBroadCastReceiver();
接收:
public class sdBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println(intent.getDataString()+"\n"+"__________action_"+action);
String path = intent.getData().toString()
.substring("file://".length());
if(action.equals(Intent.ACTION_MEDIA_EJECT)){
..
}else if(action.equals(Intent.ACTION_MEDIA_MOUNTED)){
textLog.setText("發現SD卡");
}else if(action.equals(Intent.ACTION_MEDIA_REMOVED)){
textLog.setText("有SD卡拔出");
}else if(action.equals(Intent.ACTION_MEDIA_BAD_REMOVAL)){
textLog.setText("SD卡未正常移出");
}
}
}
當然還有unregisterReceiver(receiverSD);取消註冊。