如何知道手机正在充电 [英] How to know if the phone is charging
本文介绍了如何知道手机正在充电的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道(throught我的应用程序),如果Android设备正在充电。有任何想法吗?谢谢
i need to know (throught my app) if the Android device is charging. Any ideas? thanks
推荐答案
有一件事我发现是,如果你的手机有100%的电量,你不会得到充电通知。有些人意味着充别人的意思,当有外部电源,无论是其100%与否。我集中到一条,如果任何条件为真,那么我返回true。
One thing I found out is that if your phone has 100% battery level you won't get the charging notification. Some people mean charging and others mean when it has external power, whether its 100% or not. I lumped these into one and if any condition is true then I return true.
public static boolean isPhonePluggedIn(Context context){
boolean charging = false;
final Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean batteryCharge = status==BatteryManager.BATTERY_STATUS_CHARGING;
int chargePlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if (batteryCharge) charging=true;
if (usbCharge) charging=true;
if (acCharge) charging=true;
return charging;
}
这篇关于如何知道手机正在充电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文