Android USB识别开发

一、全局广播监听

public class UDiskReceiver extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent intent) {
        if(Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())){
            Toast.makeText(context, "U盘插入", Toast.LENGTH_SHORT).show();
        } else if(Intent.ACTION_MEDIA_REMOVED.equals(intent.getAction())){
            Toast.makeText(context, "无介质", Toast.LENGTH_SHORT).show();
        } else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(intent.getAction())) {
            Toast.makeText(context, "U盘移除", Toast.LENGTH_SHORT).show();
        }
    }
}

    
        
        
        
        
    

二、代码中注册监听

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addDataScheme("file");
registerReceiver(receiver, intentFilter);
BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())) {
            tvText.setText("USB状态:U盘插入");
        } else if (Intent.ACTION_MEDIA_REMOVED.equals(intent.getAction())) {
            tvText.setText("USB状态:无介质");
        } else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(intent.getAction())) {
            tvText.setText("USB状态:U盘移除");
        }
    }
};

三、USB链接状态判断-方法一

public boolean isUDiskConnected() {
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    // 获取连接usb设备列表
    HashMap deviceList = manager.getDeviceList();
    for (String key : deviceList.keySet()) {
        UsbDevice usbDevice = deviceList.get(key);
        if (usbDevice != null) {
            for (int i = 0; i < usbDevice.getInterfaceCount(); i++) {
                UsbInterface usbInterface = usbDevice.getInterface(i);
                // 获取usb设备类型,判断当前连接的usb设备是否为存储设备(u盘或读卡器)
                int interfaceClass = usbInterface.getInterfaceClass();
                if (interfaceClass == UsbConstants.USB_CLASS_MASS_STORAGE) {
                    return true;
                }
            }
        }
    }
    return false;
}

四、USB链接状态判断-方法二

private boolean checkUdiskExist() {
    try {
        StorageManager sm = (StorageManager) getBaseContext().getSystemService(Context.STORAGE_SERVICE);
        Method volumeState = (StorageManager.class).getMethod("getVolumeState", String.class);
        //U盘路径
        String path = get("vold.udisk_mount_path", "");
        //U盘状态
        String udiskState = (String) volumeState.invoke(sm, path);
        //是否有读写权限
        if (android.os.Environment.MEDIA_MOUNTED.equals(udiskState)) {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

public static String get(String key, String def) {
    try {
        Class cls = Class.forName("android.os.SystemProperties");
        Method m = cls.getDeclaredMethod("get", String.class, String.class);
        m.setAccessible(true);
        return (String) (m.invoke(null, key, def));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

你可能感兴趣的:(APP,Android,USB)