//注册广播,监听USB插入和拔出
private void initReceiver() {
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter intentFilter =new IntentFilter();
intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
intentFilter.addAction(ACTION_USB_PERMISSION);
USBBroadCastReceiver usbBroadCastReceiver =new USBBroadCastReceiver(this);
registerReceiver(usbBroadCastReceiver, intentFilter);
}
/**
* Author : cc
* Time : 2019/5/10 10:07
* Description : USB 广播
*/
public class USBBroadCastReceiverextends BroadcastReceiver {
private UsbListenerusbListener;
public static final StringACTION_USB_PERMISSION ="com.yzkj.switching.USB_PERMISSION";
public USBBroadCastReceiver(UsbListener usbListener) {
this.usbListener = usbListener;
}
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (action ==null)
return;
switch (action) {
case ACTION_USB_PERMISSION:
//用户授权广播
synchronized (this) {
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {//允许权限申请
if (usbListener !=null) {
usbListener.getReadUsbPermission(usbDevice);
}
}else {
if (usbListener !=null) {
usbListener.failedReadUsb(usbDevice);
}
}
}
break;
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
//USB设备插入广播
if (usbListener !=null) {
usbListener.insertUsb(usbDevice);
}
break;
case UsbManager.ACTION_USB_DEVICE_DETACHED:
//USB设备拔出广播
if (usbListener !=null) {
usbListener.removeUsb(usbDevice);
}
break;
}
}
public void setUsbListener(UsbListener usbListener) {
this.usbListener = usbListener;
}
/**
* USB 操作监听 使用的Activity 实现接口即可
*/
public interface UsbListener {
//USB 插入
void insertUsb(UsbDevice device_add);
//USB 移除
void removeUsb(UsbDevice device_remove);
//获取读取USB权限
void getReadUsbPermission(UsbDevice usbDevice);
//读取USB信息失败
void failedReadUsb(UsbDevice usbDevice);
}
}
/**
* 下载文件到U盘 获取权限之后读写
*
* @param context
* @param hashMap
*/
public static void UsbFileWrite(Context context, HashMap hashMap, PendingIntent pendingIntent) {
//USB管理器
UsbManager mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
try {
UsbMassStorageDevice[] storageDevices = UsbMassStorageDevice.getMassStorageDevices(context);
for (UsbMassStorageDevice storageDevice : storageDevices) {
//一般手机只有一个USB设备
// 申请USB权限
if (!mUsbManager.hasPermission(storageDevice.getUsbDevice())) {
mUsbManager.requestPermission(storageDevice.getUsbDevice(), pendingIntent);
break;
}
// 初始化
storageDevice.init();
// 获取分区
List partitions = storageDevice.getPartitions();
if (partitions.size() ==0) {
ToastUtil.show(context, "未检测到U盘,请重新连接");
return;
}
// 仅使用第一分区
FileSystem fileSystem = partitions.get(0).getFileSystem();
boolean isExists =false;
UsbFile root = fileSystem.getRootDirectory();
UsbFile[] files = root.listFiles();
for (UsbFile file : files) {
if (file.isDirectory() && file.getName().equals(ROOTPATH)) {
isExists =true;
for (UsbFile file1 : file.listFiles()) {
for (String key : hashMap.keySet()) {
for (UsbFile ZL_VIDEO : file1.listFiles()) {
if (ZL_VIDEO.getName().equals(key)) {
ZL_VIDEO.delete();
break;
}
}
if (file1.isDirectory() && file1.getName().equals(RADIOPATH) && key.contains(".amr")) {
UsbFile newFile = file1.createFile(key);
// 写文件到U盘
// OutputStream os = new UsbFileOutputStream(newFile);
OutputStream os = UsbFileStreamFactory.createBufferedOutputStream(newFile, fileSystem);
os.write(hashMap.get(key));
os.close();
}else if (file1.isDirectory() && file1.getName().equals(XMLPATH) && !key.contains(".amr")) {
UsbFile newFile = file1.createFile(key);
OutputStream os = UsbFileStreamFactory.createBufferedOutputStream(newFile, fileSystem);
os.write(hashMap.get(key));
os.close();
}
}
}
}
}
if (!isExists) {
UsbFile usbFile = root.createDirectory(ROOTPATH);
usbFile.createDirectory(XMLPATH);
usbFile.createDirectory(RADIOPATH);
}
storageDevice.close();
/* // 写文件到本地
// InputStream is = new UsbFileInputStream(newFile);
InputStream is = UsbFileStreamFactory.createBufferedInputStream(newFile, fileSystem);
byte[] buffer = new byte[fileSystem.getChunkSize()];
int len;
File sdFile = new File("/sdcard/111");
sdFile.mkdirs();
FileOutputStream sdOut = new FileOutputStream(sdFile.getAbsolutePath() + "/" + newFile.getName());
while ((len = is.read(buffer)) != -1) {
sdOut.write(buffer, 0, len);
}
is.close();
sdOut.close();*/
}
}catch (Exception e) {
ToastUtil.show(context, "无法识别U盘,请重新连接");
}
}
}
网友评论