根据http://developer.android.com/reference/android/hardware/usb/UsbManager.html
您找不到用于获取此信息的开放 API。但是,从源代码中我们可以看到这两个函数:
/**
* Name of the MTP USB function.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
*
* {@hide}
*/
public static final String USB_FUNCTION_MTP = "mtp";
/**
* Name of the PTP USB function.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
*
* {@hide}
*/
public static final String USB_FUNCTION_PTP = "ptp";
/**
* Returns the current default USB function.
*
* @return name of the default function.
*
* {@hide}
*/
public String getDefaultFunction() {
String functions = SystemProperties.get("persist.sys.usb.config", "");
int commaIndex = functions.indexOf(',');
if (commaIndex > 0) {
return functions.substring(0, commaIndex);
} else {
return functions;
}
}
/**
* Sets the current USB function.
* If function is null, then the current function is set to the default function.
*
* @param function name of the USB function, or null to restore the default function
* @param makeDefault true if the function should be set as the new default function
*
* {@hide}
*/
public void setCurrentFunction(String function, boolean makeDefault) {
try {
mService.setCurrentFunction(function, makeDefault);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in setCurrentFunction", e);
}
}
不建议使用隐藏的 API,但这可能会解决您的问题。如果您想知道如何使用隐藏的 API,请参考此链接:
http ://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction/