Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检测android手机充电时消耗的电流_Android - Fatal编程技术网

检测android手机充电时消耗的电流

检测android手机充电时消耗的电流,android,Android,我想开发一个小应用程序,显示手机在充电过程中所消耗的电流。电话是通过A/C插入的,正在拔1A 根据我的研究,现有的API有: 但是,这些信息只会告诉您它是通过USB还是A/C充电,以及它的当前状态 我想做一些测试,查看不同电缆和空调适配器的充电率。您可以使用以下方法检查电池电量 private static int checkBatteryLevel(Context context) { IntentFilter ifilter = new IntentFilter(

我想开发一个小应用程序,显示手机在充电过程中所消耗的电流。电话是通过A/C插入的,正在拔1A

根据我的研究,现有的API有:

但是,这些信息只会告诉您它是通过USB还是A/C充电,以及它的当前状态


我想做一些测试,查看不同电缆和空调适配器的充电率。

您可以使用以下方法检查电池电量

    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;
    }
您可以使用以下接收器来检测连接和断开

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)) {

        } 

    }
}
而且在舱单上

<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>

然后,连接后,使用上述方法计算电池电量。断开连接后,计算电池电量。从电池的时间和价值的差异。你可以计算你想要的任何东西。

希望这有帮助