1、监听UsbManager.ACTION_USB_STATE
public class UsbManager {
private static final String TAG = "UsbManager";
/**
* 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_HOST_CONNECTED} boolean indicating whether USB is connected or
* disconnected as host.
* <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>
* If the sticky intent has not been found, that indicates USB is disconnected,
* USB is not configued, MTP function is enabled, and all the other functions are disabled.
*
* {@hide}
*/
public static final String ACTION_USB_STATE =
"android.hardware.usb.action.USB_STATE";
public static final String USB_CONNECTED = "connected";
public static final String USB_FUNCTION_MTP = "mtp";
public static final String USB_FUNCTION_PTP = "ptp";
public static final String USB_FUNCTION_ADB = "adb";
Android通过监听广播可以获取USB的连接状态,该广播为UsbManager.ACTION_USB_STATE。查看UsbManager源码,此action常量含有注解{@hide},不能直接调用。通过监听该广播可以获取多个参数值,其中以下几个是可以用到的:USB_CONNECTED表示USB连接状态,USB_FUNCTION_MTP 表示传输文件模式,USB_FUNCTION_PTP 表示传输照片模式,USB_FUNCTION_ADB 表示USB调试模式。
public static final String ACTION_USB_STATE = "android.hardware.usb.action.USB_STATE";
public static final String USB_FUNCTION_MTP = "mtp";
public static final String USB_FUNCTION_PTP = "ptp";
public static final String USB_FUNCTION_ADB = "adb";
public void judgeUsbConnected() {
boolean connected = true;
try {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_USB_STATE);
Intent intent = ExecutorApp.getInstance().registerReceiver(null, intentFilter);
/** true - USB连接;false - USB未连接 或 电源充电 */
connected = intent.getBooleanExtra(USB_CONNECTED, false);
/** true - USB传输文件模式 */
boolean function_mtp = intent.getBooleanExtra(USB_FUNCTION_MTP, false);
/** true - USB传输图片模式 */
boolean function_ptp = intent.getBooleanExtra(USB_FUNCTION_PTP, false);
/** true - adb模式(USB调试) */
boolean function_adb = intent.getBooleanExtra(USB_FUNCTION_ADB , false);
} catch (Throwable e) {
e.printStackTrace();
}
}
调用结果:
- 1.连接电脑,USB传输文件模式:
connected = true
function_mtp = true
function_ptp = false
function_adb = false - 2.连接电脑,仅充电模式:
connected = true
function_mtp = false
function_ptp = false
function_adb = false - 3.连接电源
connected = false
function_mtp = false/true – 不准
function_ptp = false
function_adb = false/true – 不准 - 4.未连接
connected = false
function_mtp = false
function_ptp = false
function_adb = false
经多种机型测试,判断设备是否为USB传输模式,应该判断:connected=true && function_mtp=true
2、反射获取USB传输模式
通过反射获取android.os.SystemProperties的get方法,其中,字段 sys.usb.config 可作为传输模式的判断。
public void getMtpMode() {
Class classType = null;
String function = null;
try {
classType = Class.forName("android.os.SystemProperties");
Method getMethod = classType.getDeclaredMethod("get", new Class[]{String.class});
/** function = mtp | ptp | adb | none ... */
function = (String) getMethod.invoke(classType, new Object[]{"sys.usb.config"});
} catch (Exception e) {
e.printStackTrace();
}
}
也可以用adb 命令获取设备的prop属性
adb shell getprop > c:\prop.log
其中一个属性值sys.usb.config,即为传输模式。