简体   繁体   English

如何在android中检测外部设备

[英]How to detect external device in android

I want to write a program to detect any external device with which my android phone is connected. 我想写一个程序来检测我的android手机所连接的任何外部设备。 eg if I connect to computer to share though usb mass storage or if I connect for charging. 例如,如果我连接到计算机以共享通过USB大容量存储或如果我连接充电。

Which API or package in android is responsible for hardware interfaces. android中的哪个API或包负责硬件接口。 How can I detect any of external device if connected to my android phone. 如果连接到我的Android手机,如何检测任何外部设备。

似乎对于2.3.4和3.1,您使用USB API

While USB connection is not perfectly synonymous with external power, that might be a reasonable way to detect it for the majority of devices using legacy releases. 虽然USB连接并不完全与外部电源同义,但对于使用旧版本的大多数设备来说,这可能是一种合理的方法来检测它。

The ACTION_BATTERY_CHANGED Intent has constants for both BATTERY_PLUGGED_USB and BATTERY_PLUGGED_AC. ACTION_BATTERY_CHANGED Intent具有BATTERY_PLUGGED_USB和BATTERY_PLUGGED_AC的常量。

See 看到

http://developer.android.com/reference/android/os/BatteryManager.html http://developer.android.com/reference/android/os/BatteryManager.html

You could also detect the loss of the external storage directory during USB mounting - but bear in mind first that not everyone mounts their external storage when USB connected, and that newer android releases aren't going to do an outright USB mass storage mount at all. 您还可以在USB安装过程中检测到外部存储目录的丢失 - 但首先要记住,当USB连接时,不是每个人都安装了外部存储器,并且新的Android版本根本不会完全安装USB大容量存储器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM