更新时间:2022-01-02 22:58:37
要检查设备与AC / USB 充电,让我们试试这个,
To check device charging with AC / USB, Lets try this,
呼叫 registerReceiver(空,新的IntentFilter(Intent.ACTION_BATTERY_CHANGED))
。这将返回对 BatteryManager
定义额外让你知道,如果它是在插入或不是意图。
Call registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED))
. This will return an Intent that has extras defined on BatteryManager
to let you know if it is plugged in or not.
一些与code,
public class PowerUtil {
public static boolean isConnected(Context context) {
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}
}