我一直在尝试使用反射,以防有人插入充电器或 USB 电缆为其设备充电。谁能告诉我 android 实现了什么与硬件交互。
我对 USB 不太感兴趣 我对红色(HTC 为琥珀色)LED 更感兴趣,当我们连接设备进行充电或收到通知时,该 LED 会发光。
最佳答案
为ACTION_BATTERY_CHANGED
设置广播接收器。 Intent extra 会告诉您充电状态是什么 - 请参阅 BatteryManager了解详情。
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".receiver.PlugInControlReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
</application>
然后
public void onReceive(Context context , Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
// Do something when power connected
}
else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
// Do something when power disconnected
}
}
关于java - 当你插入充电器时,android 会调用什么服务和类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16707154/