1:实现SD卡挂载监听
private class UsbReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.endsWith("MEDIA_MOUNTED"))//OTG 已挂载成功
{
//在android中OTG加载的SD卡或U盘目录为 //mnt/udisk 或者 是storage/udisk
//可以在system/etc/vold.fstab文件中找到挂载的盘符
}
else if (action.endsWith("MEDIA_EJECT"))//OTG 已移除
{
}
}
};
2:在Oncreate 中注册广播
UsbReceiver usbRe = new UsbReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_CHECKING);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addAction(Intent.ACTION_MEDIA_SHARED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addDataScheme("file");
registerReceiver(usbRe, filter);
3:AndroidManifest.xml
添加 <uses-feature
android:name="android.hardware.usb.host"
android:required="true" />
· android:required="true":表示需要设备具有某个功能。
o 如果设备没有该功能,则程序不工作,就是可以理解的
· android:required="false":表示希望设备,最好具有某个功能
o 设备即使没有该功能,程序也应该可以正常工作
o 为了程序工作的更好,最好具有该功能。