0

I have an app that displays if an android device is being charged by a plug, a usb cable, a wireless device or not charging at all. I am using the code below, it will display the correct charging method (if any) until I charge my phone (has android 4.1) or tablet (has android 4.3) into a usb charger in a car. It will say it's charging by a plug, when it should say a usb device. What is the reason behind this?

        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = registerReceiver(null, filter);

        int chargeState = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

        if(chargeState == BatteryManager.BATTERY_PLUGGED_USB)
        {
            chargingBy = "charging via usb cable";
        }
        else if (chargeState == BatteryManager.BATTERY_PLUGGED_AC)
        {
            chargingBy = "charging via plug";
        }
        else if (chargeState == BatteryManager.BATTERY_PLUGGED_WIRELESS)
        { 
            chargingBy = "charging via wireless device";
        }
        else
        {
            chargingBy = "not charging";
        }
4

1 回答 1

1

从硬件的角度来看,交流充电器和车载充电器之间没有任何区别,因此不足为奇。这是一个相当长的阅读,但可以在此处以 1.3MB zip 文件的形式找到 USB 充电的完整规格:

电池充电 v1.2 规范和采用者协议

大概BATTERY_PLUGGED_USB是为 USB 主机保留的,而BATTERY_PLUGGED_AC任何被识别为充电器的东西。无线充电需要手机/平板电脑内的硬件支持,因此具有该功能的硬件将能够检测到它何时被使用。

于 2013-09-03T09:21:37.943 回答