【andrid笔记】MyBatteryDemo增加log信息打印以及其他电池信息

增加log信息打印

Log.d("Battery level:", "" + intent.getIntExtra("level", 0));
Log.d("Battery scale:", "" + intent.getIntExtra("scale", 0));
Log.d("Battery status:", "" + intent.getIntExtra("status", 0));
Log.d("Battery charger type:", "" + intent.getIntExtra("plugged", 0));
Log.d("Battery health:", "" + intent.getIntExtra("health",0));

Log.d("Battery present:", "" + intent.getBooleanExtra("present",false));
Log.d("Battery technology:", "" + intent.getStringExtra("technology"));

其他电池信息

//获取当前的充电器类型
batteryType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
if(1==batteryType)
    textViewType.setText("类型:"+"AC");
else if(2==batteryType)
    textViewType.setText("类型:"+"USB");
else
    textViewType.setText("类型:"+"OTHER");

//获取当前的HEALTH状态
batteryHealth = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
if(1==batteryHealth)
    textViewHealth.setText("Health:"+"unKnown");
else if(2==batteryHealth)
    textViewHealth.setText("Health:"+"GOOD");
else if(3==batteryHealth)
    textViewHealth.setText("Health:"+"OVERHEAT");
else if(4==batteryHealth)
    textViewHealth.setText("Health:"+"DEAD");
else if(5==batteryHealth)
    textViewHealth.setText("Health"+"OVER VOLTAGE");
else if(7==batteryHealth)
    textViewHealth.setText("Health:"+"COLD");
else if(6==batteryHealth)
    textViewHealth.setText("Health:"+"UNSPECIFIED FAILURE");

Intent中的数据是在哪里被设置的

以上我们可以看到需要的电池信息都是通过intent来获得的.intent是android应用程序之间进行数据交换的一种重要方式.那么intent中的数据又是在哪里设置进去的?

如下:
android\frameworks\base\services\core\java\com\android\server\Battervice.java

private void sendIntentLocked() {
        //  Pack up the values and broadcast them to everyone
        final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
                | Intent.FLAG_RECEIVER_REPLACE_PENDING);

        int icon = getIconLocked(mBatteryProps.batteryLevel);

        intent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
        intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
        intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
        intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
        intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
        intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
        intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
        intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
        intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
        intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
        intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
        intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
        intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
        intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
        intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值