且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查Android的USB连接状态

更新时间:2023-02-27 07:57:06

  

这是实际可行的。

当然。

Android的播放各种 意图 ,你可以听与的BroadcastReceiver 。其中包括:

  • ACTION_BATTERY_CHANGED (告诉你,如果设备插入USB或AC)
  • ACTION_UMS_CONNECTED ACTION_UMS_DISCONNECTED (告诉你,如果设备进入或离开USB大容量存储模式)

I was wondering if would be possible to perform an action for each time the USB status of the device changes.

For example if the user plugs in the cable and it is in sync/charge mode it performs a certain action, if the user then changes the phone into USB Memory Card mode another action is performed and if the user then disconnects the cable then the another action is done.

Is this actually possible. Any help with this would be greatly appreciated.

Thank you

Is this actually possible.

Sure.

Android broadcasts various Intents that you can listen to with a BroadcastReceiver. Among these are:

  • ACTION_BATTERY_CHANGED (tells you if device is plugged into USB or AC)
  • ACTION_UMS_CONNECTED and ACTION_UMS_DISCONNECTED (tells you if device enters or leaves USB Mass Storage mode)