安卓检测USB设备插入之后回调用户代码的小框架编写

      用户希望在视频播放Fragment中,当插入U盘时弹出文件选择框,于是做了这套框架:

      首先是继承广播BroadcastReceiver,使得插入U盘之后应用能收到,并且收到之后根据状态调用用户的回调:



import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.support.v4.util.ArrayMap;

import java.util.Map;

/**
 * Created by ChenJieZhu on 2017/9/22.
 */

public class USBManager extends BroadcastReceiver {
    private IntentFilter intentFilter;
    private USBDeviceCallBack usbDeviceCallBack;

    public USBManager(Context context){
        intentFilter = new IntentFilter();
        intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        intentFilter.addAction(USBManagerConstant.ACTION_USB_STATE);
        context.registerReceiver(this, intentFilter);
    }

    @Override

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Map<String, Boolean> usbStateParams = new ArrayMap<>();
        UsbAccessory accessory = null;
        UsbDevice device = null;
        switch(action) {
            case UsbManager.ACTION_USB_ACCESSORY_ATTACHED:
                accessory = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                if(this.usbDeviceCallBack != null) this.usbDeviceCallBack.usbAccessoryAttached(accessory);
                break;
            case UsbManager.ACTION_USB_ACCESSORY_DETACHED:
                //Name of extra for ACTION_USB_ACCESSORY_ATTACHED and ACTION_USB_ACCESSORY_DETACHED broadcasts containing the UsbAccessory object for the accessory.
                accessory = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                if(this.usbDeviceCallBack != null) this.usbDeviceCallBack.usbAccessoryDetached(accessory);
                break;
            case UsbManager.ACTION_USB_DEVICE_ATTACHED:
                device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if(this.usbDeviceCallBack != null) this.usbDeviceCallBack.usbDeviceAttached(device);
                break;
            case UsbManager.ACTION_USB_DEVICE_DETACHED:
                //Name of extra for ACTION_USB_DEVICE_ATTACHED and ACTION_USB_DEVICE_DETACHED broadcasts containing the UsbDevice object for the device.
                device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if(this.usbDeviceCallBack != null) this.usbDeviceCallBack.usbDeviceDetached(device);
                break;
            case USBManagerConstant.ACTION_USB_STATE:
                usbStateParams.put(USBManagerConstant.USB_CONNECTED, intent.getBooleanExtra(USBManagerConstant.USB_CONNECTED, false));
                usbStateParams.put(USBManagerConstant.USB_CONFIGURED, intent.getBooleanExtra(USBManagerConstant.USB_CONFIGURED, false));
                usbStateParams.put(USBManagerConstant.USB_FUNCTION_ADB, intent.getBooleanExtra(USBManagerConstant.USB_FUNCTION_ADB, false));
                usbStateParams.put(USBManagerConstant.USB_FUNCTION_RNDIS, intent.getBooleanExtra(USBManagerConstant.USB_FUNCTION_RNDIS, false));
                usbStateParams.put(USBManagerConstant.USB_FUNCTION_MTP, intent.getBooleanExtra(USBManagerConstant.USB_FUNCTION_MTP, false));
                usbStateParams.put(USBManagerConstant.USB_FUNCTION_PTP, intent.getBooleanExtra(USBManagerConstant.USB_FUNCTION_PTP, false));
                usbStateParams.put(USBManagerConstant.USB_FUNCTION_AUDIO_SOURCE, intent.getBooleanExtra(USBManagerConstant.USB_FUNCTION_AUDIO_SOURCE, false));
                usbStateParams.put(USBManagerConstant.USB_FUNCTION_MIDI, intent.getBooleanExtra(USBManagerConstant.USB_FUNCTION_MIDI, false));
                if(this.usbDeviceCallBack != null) this.usbDeviceCallBack.usbState(usbStateParams);
                break;
        }
    }

    public interface USBDeviceCallBack{
        void usbAccessoryAttached(UsbAccessory accessory);

        void usbAccessoryDetached(UsbAccessory accessory);

        void usbDeviceAttached(UsbDevice device);

        void usbDeviceDetached(UsbDevice device);

        /**@usbStateParams USB状态
         * key值传入USBManagerConstant的常量即可得到
         * 对应的usb状态值**/
        void usbState(Map<String, Boolean> usbStateParams);
    }

    public void setUsbDeviceCallBack(USBDeviceCallBack usbDeviceCallBack) {
        this.usbDeviceCallBack = usbDeviceCallBack;
    }
}


另外关键字常量类如下:

public class USBManagerConstant {
	/**
     * Broadcast Action: A sticky broadcast for USB state change events when in device mode.
     * This is a sticky broadcast for clients that includes USB connected/disconnected state,
     * <ul>
     * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected.
     * <li> {@link #USB_CONFIGURED} boolean indicating whether USB is configured. currently zero if not configured, one for configured.
     * <li> {@link #USB_FUNCTION_ADB} boolean extra indicating whether the adb function is enabled
     * <li> {@link #USB_FUNCTION_RNDIS} boolean extra indicating whether the RNDIS ethernet function is enabled
     * <li> {@link #USB_FUNCTION_MTP} boolean extra indicating whether the MTP function is enabled
     * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the PTP function is enabled
     * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the accessory function is enabled
     * <li> {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the audio source function is enabled
     * <li> {@link #USB_FUNCTION_MIDI} boolean extra indicating whether the MIDI function is enabled
     * </ul>
     */

    public static final String ACTION_USB_STATE = "android.hardware.usb.action.USB_STATE";

    /**
     * Boolean extra indicating whether USB is connected or disconnected.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
     */
    public static final String USB_CONNECTED = "connected";

    /**
     * Boolean extra indicating whether USB is configured.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
     */
    public static final String USB_CONFIGURED = "configured";

    /**
     * Boolean extra indicating whether confidential user data, such as photos, should be
     * made available on the USB connection. This variable will only be set when the user
     * has explicitly asked for this data to be unlocked.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
     */
    public static final String USB_DATA_UNLOCKED = "unlocked";

    /**
     * A placeholder indicating that no USB function is being specified.
     * Used to distinguish between selecting no function vs. the default function in {@link #setCurrentFunction(String)}.
     */
    public static final String USB_FUNCTION_NONE = "none";

    /**
     * Name of the adb USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_ADB = "adb";

    /**
     * Name of the RNDIS ethernet USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_RNDIS = "rndis";

    /**
     * Name of the MTP USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_MTP = "mtp";

    /**
     * Name of the PTP USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_PTP = "ptp";

    /**
     * Name of the audio source USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source";

    /**
     * Name of the MIDI USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_MIDI = "midi";
}



用户创建USBManager类的对象后,实现USBDeviceCallBack,然后调用setUsbDeviceCallBack该对象。待USB设备状态变化时即可被回调调用用户些的代码


猜你喜欢

转载自blog.csdn.net/cjzjolly/article/details/78617632