版权声明
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/16258670.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
前言
此博客讲解Usb插入的设备信息获取与注意事项
注意事项
Usb设备的插拔会导致Activity重建,这是正常的。Android在外接一些设备后默认认为页面的状态是需要更新的,比如操作方式,多屏状态等等。我们可以在AndroidManifest.xml设置我们希望重建与不希望重建的配置。如下:
<activity android:name=".ui.aging.autoaging.AutoAgingActivity" android:exported="false" android:configChanges="keyboard|keyboardHidden"> </activity>
configChanges这个属性控制着,我们activity在某些情况下是不需要重新创建activity的,这里的不重新创建意味着activity的生命周期将保持不变,不会重新走任何一个生命周期包括onResume.这里设置方向keyboard和尺寸keyboardHidden,就会让插拔Usb键盘鼠标的时候部重建Activity
USB线
private fun initUsbReceiver() { val filter = IntentFilter() filter.addAction("android.hardware.usb.action.USB_STATE") mUsbBroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val action = intent.action if (action == "android.hardware.usb.action.USB_STATE") { val connected = intent.extras.getBoolean("connected") if (connected) { Toast.makeText(this@UsbActivity, "USB已连接", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this@UsbActivity, "USB已断开", Toast.LENGTH_SHORT).show() } } } } registerReceiver(mUsbBroadcastReceiver, filter) } private fun unregisterUsbReceiver() { unregisterReceiver(mUsbBroadcastReceiver) }
普通USB设备(U盘)
private fun initUsbDeviceReceiver() { val filter = IntentFilter() filter.addAction("android.hardware.usb.action.USB_DEVICE_ATTACHED") filter.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED") mUsbBroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val action = intent.action if (action == "android.hardware.usb.action.USB_DEVICE_ATTACHED") { val usbManager: UsbManager = getSystemService(Context.USB_SERVICE) as UsbManager val deviceList = usbManager.deviceList Log.e("zh", "设备连接 设备数目:${deviceList.size}") deviceList.forEach { Log.e("zh", " 设备:${it}") } } if (action == "android.hardware.usb.action.USB_DEVICE_DETACHED") { val usbManager: UsbManager = getSystemService(Context.USB_SERVICE) as UsbManager val deviceList = usbManager.deviceList Log.e("zh", "设备断开 设备数目:${deviceList.size}") deviceList.forEach { Log.e("zh", " 设备:${it}") } } } } registerReceiver(mUsbBroadcastReceiver, filter) } private fun unregisterUsbDeviceReceiver() { unregisterReceiver(mUsbBroadcastReceiver) }
输入型USB设备(鼠标,键盘)
private val mInputManager by lazy { getSystemService(Context.INPUT_SERVICE) as InputManager }private fun initEnterUsbDeviceReceiver() { mInputDeviceListener = object : InputManager.InputDeviceListener { override fun onInputDeviceAdded(deviceId: Int) { val device = mInputManager.getInputDevice(deviceId) Log.e("zh", "onInputDeviceAdded: ${device.name}") } override fun onInputDeviceRemoved(deviceId: Int) { } override fun onInputDeviceChanged(deviceId: Int) { } } mInputManager.registerInputDeviceListener(mInputDeviceListener, null) } private fun unregisterEnterUsbDeviceReceiver() { mInputManager.unregisterInputDeviceListener(mInputDeviceListener) }
U盘文件操作
推荐使用,下面这个第三方框架
implementation 'com.github.mjdev:libaums:0.5.5'
参考代码
private fun getMassStorageDevices(){
val devices = UsbMassStorageDevice.getMassStorageDevices(this)
for (itemDevice in devices){
itemDevice.init()
if (itemDevice == null || itemDevice.getPartitions() == null ||
itemDevice.getPartitions().get(0) == null ||
itemDevice.getPartitions().get(0).getFileSystem() == null) {
return
}
//getPartitions为得到分区
//get(0)是挂载的USB设备数量
val fileSystem = itemDevice.getPartitions().get(0).getFileSystem()
//根目录
val root = fileSystem.rootDirectory
//获取当前目录文件列表
val fileArray = root.listFiles()
for (file in fileArray){
Log.e("zh", "文件名称 = ${file.name}")
Log.e("zh", "是否是根目录 = ${file.isRoot}")
Log.e("zh", "是否是目录 = ${file.isDirectory}")
Log.e("zh", "上一级目录 = ${file.parent}")
Log.e("zh", "文件长度 = ${file.length}")
//val in = UsbFileInputStream(file); 输入流
//val ou = UsbFileOutputStream(file) 输出流
//file.createFile() 创建文件
//file.createDirectory() 创建目录
//file.delete() 删除文件
}
}
}
End
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/16258670.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。