简体   繁体   English

为Android手机充电时检测当前消耗的电流

[英]Detect current current drawn when charging android phone

I would like to develop a small app that shows the current current being drawn by the phone during a charge. 我想开发一个小应用程序,以显示手机在充电过程中消耗的电流。 eg The phone is plugged in via A/C and is pulling 1A. 例如,电话已通过A / C插入并拉1A。

From my research there are the existing APIs: 根据我的研究,有现有的API:

http://developer.android.com/reference/android/os/BatteryManager.html http://developer.android.com/training/monitoring-device-state/battery-monitoring.html http://developer.android.com/reference/android/os/BatteryManager.html http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

However these will only tell you whether it is charging via USB or A/C and it's current status. 但是,这些信息只会告诉您它是通过USB还是A / C进行充电以及当前状态。

I would like to do some tests to view the charge rate with different cables and A/C adaptors. 我想做一些测试以查看不同电缆和空调适配器的充电率。

You can use the following method to check battery level 您可以使用以下方法检查电池电量

    private static int checkBatteryLevel(Context context) {
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, ifilter);

        // Are we charging / charged?
        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;

        // If the device is charging then set the state as charging and return.
        if (isCharging)
            return 100;

        // Get the battery level.
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        // To get the battery level in %
        int batteryLevel = (level * 100) / scale;
        Log.e(Constants.TAG, "Battery Level: " + batteryLevel);

        return batteryLevel;
    }

And you can use the following receiver to detect getting connected and disconnected 您可以使用以下接收器来检测连接和断开连接

public class BatteryInformationReceiver extends BroadcastReceiver {

    private static final String ACTION_POWER_CONNECTED = "android.intent.action.ACTION_POWER_CONNECTED";
    private static final String ACTION_POWER_DISCONNECTED = "android.intent.action.ACTION_POWER_DISCONNECTED";

    @Override
    public void onReceive(Context context, Intent intent) {

        String intentAction = intent.getAction();
        if (intentAction.equalsIgnoreCase(ACTION_POWER_CONNECTED)) {

        } else if (intentAction.equalsIgnoreCase(ACTION_POWER_DISCONNECTED)) {

        } 

    }
}

and in manifest 并在清单中

<receiver android:name="your.package.receivers.BatteryInformationReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
            </intent-filter>
        </receiver>

and then, on getting connected, calculate the battery level using the above method. 然后,在连接时,使用上述方法计算电池电量。 and after getting disconnected, calculate the battery level. 断开连接后,计算电池电量。 and from the difference between times and value of battery. 以及时间和电池价值之间的差异。 you can calculate whatever you want. 您可以计算任何您想要的。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM