3

现在,我只想在用户访问应用程序时获得某种电池状态指示。例如,如果设备已插入,则设备应显示正在充电及其所处的水平。如果设备已拔下,则不应显示任何内容。如果设备未充电且电池电量低,则应出现低电量指示。

我已经设置了大部分代码,但问题是,例如,如果我运行应用程序并且设备已插入,屏幕上会显示充电指示。如果我拔下设备,充电不再可见,但如果我重新插入设备,充电仍然不可见。当设备插入或拔出、电池电量过高或过低时,应始终显示电池警报。应始终显示信息的变化。

所以这是我的广播接收器:

  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver()  {

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

            //Battery level
            int level = intent.getIntExtra("level", 0);

            //Plugged in Status
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

            //Battery Status
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

            //If the device is charging or contains a full status, it's charging
            boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                    status == BatteryManager.BATTERY_STATUS_FULL;

            //If the device isCharging and plugged in, then show that the battery is charging
            TextView batteryTextView = ((TextView) findViewById(R.id.charging));
            TextView batteryLowTextView = ((TextView) findViewById(R.id.low_battery));
            ImageView batteryLowImageView= ((ImageView) findViewById(R.id.low_battery_icon));
            ImageView batteryImageView =((ImageView) findViewById(R.id.charging_battery_icon));

            if (isCharging && plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB) {


                //Gets the 'last synced' string and sets to datetime of the last sync
                Resources resources = context.getResources();
                String chargingString = String.format(resources.getString(R.string.charging), level);

                //Dynamically sets the value of the battery level
                batteryLowTextView.setVisibility(TextView.INVISIBLE);
                batteryLowImageView.setVisibility(ImageView.INVISIBLE);
                batteryTextView.setText(chargingString + "%");

            } else if (level < LOW_BATTERY_LEVEL && !isCharging) {

                Resources resources = context.getResources();
                String lowBatteryString = String.format(resources.getString(R.string.low_battery));

                batteryTextView.setVisibility(TextView.INVISIBLE);
                batteryImageView.setVisibility(ImageView.INVISIBLE);
                batteryLowTextView.setText(lowBatteryString);

            } else if (!isCharging && level > LOW_BATTERY_LEVEL) {

                //do nothing
                batteryTextView.setVisibility(TextView.GONE);
                batteryImageView.setVisibility(ImageView.GONE);

            }

        }

    };

在我的 OnCreate 方法中,我调用

 //calls registerReceiver to receive the broadcast for the state of battery
            this.registerReceiver(this.mBatInfoReceiver,new
                    IntentFilter(Intent.ACTION_BATTERY_CHANGED));

所以,我想知道我做错了什么?是布尔人吗?

4

2 回答 2

5
  1. 您的if-then-else逻辑有很多漏洞(并且有@Vikram 提到的问题)。这可能导致接收器最终什么都不做。尝试简化逻辑以消除所有漏洞。
  2. 当您更新文本视图时,您还必须记住首先使其(及其图像视图)可见。

这是您的 if-then-else 逻辑的替代品:

        if (isCharging) {

            //Gets the 'last synced' string and sets to datetime of the last sync
            Resources resources = context.getResources();
            String chargingString = String.format(resources.getString(R.string.charging), level);

            //Dynamically sets the value of the battery level
            batteryLowTextView.setVisibility(TextView.INVISIBLE);
            batteryLowImageView.setVisibility(ImageView.INVISIBLE);
            batteryTextView.setText(chargingString + "%");
            batteryTextView.setVisibility(TextView.VISIBLE);
            batteryImageView.setVisibility(ImageView.VISIBLE);

        } else if (level <= LOW_BATTERY_LEVEL) {

            Resources resources = context.getResources();
            String lowBatteryString = String.format(resources.getString(R.string.low_battery));

            batteryTextView.setVisibility(TextView.INVISIBLE);
            batteryImageView.setVisibility(ImageView.INVISIBLE);
            batteryLowTextView.setText(lowBatteryString);
            batteryLowTextView.setVisibility(TextView.VISIBLE);
            batteryLowImageView.setVisibility(ImageView.VISIBLE);

        } else {

            //show nothing
            batteryTextView.setVisibility(TextView.GONE);
            batteryImageView.setVisibility(ImageView.GONE);
            batteryLowTextView.setVisibility(TextView.GONE);
            batteryLowImageView.setVisibility(ImageView.GONE);
        }
于 2014-10-03T06:50:35.843 回答
2

AND isChargingBatteryManager.BATTERY_PLUGGED_AC这里:

if (isCharging && plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB) 

您需要使用OR以下BatteryManager控件和AND结果isCharging

if (isCharging && 
   (plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB))

如果您需要知道设备本质上是插入AC还是USB,您可以使用此方法。但是,该设备可以插入到BATTERY_PLUGGED_WIRELESS. 另一件事是,您的isCharging变量是否已经涵盖了您要通过以下方式找出的内容:

(plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB)

我强烈建议阅读此页面并进行调试以控制您操作的变量的值。

于 2014-10-03T07:06:36.420 回答