【问题标题】:Detect current current drawn when charging android phone检测为安卓手机充电时消耗的电流
【发布时间】:2014-03-03 02:19:49
【问题描述】:

我想开发一个小应用程序,显示充电期间手机消耗的电流。例如,手机是通过空调插入的,并且正在拉 1A。

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

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

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

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

        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>
    

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多