gpt4 book ai didi

android - 如何检测电源连接状态?

转载 作者:IT老高 更新时间:2023-10-28 22:15:41 27 4
gpt4 key购买 nike

当 USB 或交流电源连接到 Android 手机时,是否有一种简单的方法可以收到通知?

最佳答案

在 AndroidManifest.xml 中

<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 class PlugInControlReceiver extends BroadcastReceiver {
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
}
}
}

关于android - 如何检测电源连接状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3267161/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com