How to tell if USB is connected to android device?
I've written an Android application and I would like the application to be able to recognize when the Android device is plugged into a PC via the USB or even better, know when it is connected and adb logcat is running. Is this possible?
My purpose is to write traces to a separate circular buffer (which 开发者_如何学Cwould get written to the sdcard occasionally) except when the device is connected e.g., via Eclipse and logcat is running - in that case, I would like to display the traces as part of logcat as they occur.
According to the Intent doc, you have several intents fired related to USB Storage:
ACTION_MEDIA_SHARED
Broadcast Action: External media is unmounted because it is being shared via USB mass storage.ACTION_UMS_CONNECTED
Broadcast Action: The device has entered USB Mass Storage mode.ACTION_UMS_DISCONNECTED Broadcast Action: The device has exited USB Mass Storage mode.
You can write a broadcast receiver to know when the device will be connected via USB.
Hope this helped.
精彩评论